Note that there are some explanatory texts on larger screens.

plurals
  1. POSend data between controllers
    primarykey
    data
    text
    <p>I have two models: <code>Activity</code> and <code>Comment</code>. <code>Comment</code> <code>belongs_to :activity</code> and <code>Activity</code> <code>has_many :comments</code>. For building activities, I've followed this <a href="http://railscasts.com/episodes/407-activity-feed-from-scratch" rel="nofollow">Railscast (Activity feed from scratch)</a></p> <p>So, I'm using <code>activity_presenter.rb</code></p> <pre><code>class ActivityPresenter &lt; SimpleDelegator attr_reader :activity def initialize(activity, view) super(view) @activity = activity end def render_activity div_for activity do link_to((gravatar_for activity.user, {size: 20}) + activity.user.name, activity.user) + ' ' + render_partial end end def render_partial locals = {activity: activity, presenter: self} locals[activity.trackable_type.underscore.to_sym] = activity.trackable render partial_path, locals end def partial_path partial_paths.detect do |path| lookup_context.template_exists? path, nil, true end || raise("No partial found for activity in #{partial_paths}") end def partial_paths [ "activities/#{activity.trackable_type.underscore}/#{activity.action}", "activities/#{activity.trackable_type.underscore}", "activities/activity" ] end end </code></pre> <p>which renders <code>_activities_feed.rb</code> partial with <code>@activities.each do |activity|</code> condition.</p> <p>In this partial I have 3 blocks:</p> <ul> <li><code>activity</code> with <code>form for activity</code> for updating purposes</li> <li><code>activity.comments</code> block with <code>each do |comment|</code></li> <li><code>activity.comments.new</code> (form)</li> </ul> <p>And this is a problem. I need to render <code>Activity.comments</code> and form for creating a new comment from <code>comments</code> controller, because I have 2 forms in one view and 2 <code>submit</code> buttons, which call 2 <code>update</code> or <code>create</code> actions. And, of course, I need it for making app logic better.</p> <p>But I can't send <code>activity</code> from <code>@activities.each do |activity|</code>, which I use everywhere in this partial to <code>comments_controller</code>.</p> <p>I've tried to add <code>@activity = Activity.find(params[:id])</code>, <code>@comment = Activity.comments.new(params[:activity_id])</code>, <code>@comments = Activity.comments.all</code> to <code>activities</code> and <code>comments</code> controllers, and also changed <code>activity</code> to <code>@activity</code> and <code>activity.comments.new</code> to <code>@comment</code>. But it doesn't help. I think, that I don't understand the basic logic of interaction between controllers, but can't find my mistakes.</p> <p>So, my <code>activities_controller</code>'s <code>index</code> action is:</p> <pre><code>@activities = Activity.order('updated_at DESC') </code></pre> <p>And in <code>comments_controller</code> I only have <code>create</code> and <code>destroy</code> actions, which work ok.</p> <p>Thanks for any help!</p> <p>UPD:</p> <p>This is my <code>_activities_feed.rb</code> structure:</p> <pre><code>&lt;% @activities.each do |activity| %&gt; &lt;%= form_for activity, remote: true do |a| %&gt; &lt;%= ActivityPresenter.new(activity, self).render_activity %&gt; . . . &lt;%= link_to ... class: 'add_comment', remote: true %&gt; # Shows activity.new.comment form &lt;%= link_to ... 'Edit' ... %&gt; # edit activity link &lt;%= a.submit 'Update' %&gt; # first submit button for submitting activity changes . . . &lt;% activity.comments.each do |comment| %&gt; # activity.comments block &lt;%= comment.text %&gt; &lt;%= link_to comment.user.name, comment.user %&gt; &lt;% end %&gt; . . . &lt;%= form_for activity.comments.new, remote: true do |f| %&gt; # form for adding comment &lt;%= f.text_area :text, 'rows' =&gt; '2', class: 'comment_text' %&gt; &lt;%= f.button 'Cancel', class: 'btn comment_cancel', type: 'button' %&gt; &lt;%= f.submit 'Post', class: 'btn btn-primary', controller: 'comments' %&gt; # second submit button &lt;%= f.hidden_field :activity_id, value: activity.id %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload