Note that there are some explanatory texts on larger screens.

plurals
  1. POFrustrated With Nesting in Ruby on Rails
    text
    copied!<p>This is my first post here and to be honest, I don't tend to ask many questions, but something has been irritating me for quite some time involving nesting in Ruby on Rails.</p> <p>I have a <code>class Thought</code> (which are members' individual posts) that are paginated as <code>@share_items</code>. I am including a like button that will allow members to like other members' posts (like Facebook). The like button has the <code>class Opinion</code>.</p> <p>My code thus far:</p> <pre><code>Class Member has_many :thoughts, dependent: :destroy has_many :opinions, foreign_key: "fan_id", dependent: :destroy has_many :fans, through: :opinions Class Thought belongs_to :member has_many :opinions, foreign_key: "like_id", dependent: :destroy has_many :likes, through: :opinions validates :member_id, presence: true Class Opinion attr_accessible :like_id belongs_to :fan, class_name: "Member" belongs_to :like, class_name: "Thought" validates :fan_id, presence: true validates :like_id, presence: true Member Model def share Thought.from_members_authenticated(self) end MemberPages Controller def homepage @share_items = registered_member.share.paginate(page: params[:page]) end </code></pre> <p>Partial <code>_share.html.erb</code>:</p> <pre><code>&lt;% if @share_items.any? %&gt; &lt;ol class="member_shares"&gt; &lt;%= render 'shared/share_item', collection: @share_items %&gt; &lt;/ol&gt; &lt;%= will_paginate @share_items %&gt; &lt;% end %&gt; </code></pre> <p>Which renders partial <code>_share_item.html.erb</code>:</p> <pre><code>&lt;li id="&lt;%= share_item.id %&gt;"&gt; content... &lt;%= render 'thoughts/like_form' %&gt; ...content &lt;/li&gt; </code></pre> <p>Everything goes smoothly until I try to render the form <code>thoughts/like_form</code> inside paginated <code>@share_items</code> (<code>class Thought</code>). It has bizarre and unexplainable results (to me). Here are the codes leading up to the form.</p> <pre><code>Member Model def liked?(random_thought) opinions.find_by_like_id(random_thought.id) end def like!(random_thought) opinions.create!(like_id: random_thought.id) end def unlike!(random_thought) opinions.find_by_like_id(random_thought.id).destroy end OpinionsController respond_to :html, :js def create @thought = Thought.find(params[:opinion][:like_id]) registered_member.like!(@thought) respond_with @thought end def destroy @thought = Opinion.find(params[:id]).like registered_member.unlike!(@thought) respond_with @thought end end </code></pre> <p>And finally the form <code>thoughts/like_form</code>:</p> <pre><code>&lt;div id="like_form"&gt; &lt;% if registered_member.liked?(@thought) %&gt; &lt;%= render 'thoughts/unlike' %&gt; &lt;% else %&gt; &lt;%= render 'thoughts/like' %&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> <p>which will render either <code>thoughts/unlike</code>:</p> <pre><code>&lt;%= form_for(registered_member.opinions.find_by_like_id(@thought), html: { method: :delete }, remote: true) do |f| %&gt; &lt;%= f.submit "Unlike", class: "btn btn-small btn-default" %&gt; &lt;% end %&gt; </code></pre> <p>or: <code>thoughts/like</code>:</p> <pre><code>&lt;%= form_for(registered_member.opinions.build(like_id: @thought.id), remote: true) do |f| %&gt; &lt;div&gt;&lt;%= f.hidden_field :like_id %&gt;&lt;/div&gt; &lt;%= f.submit "Like", class: "btn btn-small btn-primary" %&gt; &lt;% end %&gt; </code></pre> <p>Concluding with <code>opinions/create.js.erb</code>:</p> <pre><code>$("#like_form&lt;%= @thought.id %&gt;").html("&lt;%= escape_javascript(render('shared/unlike')) %&gt;") $("#likes").html('&lt;%= @thought.likes.count %&gt;') </code></pre> <p>and <code>opinions/destroy.js.erb</code>:</p> <pre><code>$("#like_form&lt;%= @thought.id %&gt;").html("&lt;%= escape_javascript(render('shared/like')) %&gt;") $("#likes").html('&lt;%= @thought.likes.count %&gt;') </code></pre> <p>Notice I do something fishy to the js... I try to tell the like form to target a specific thought (member's share). Note: This has been the most successful thus far, but no cigar. It adds fan_id (member) and like_id (thought) to the database and renders the unlike button. But it has unpredicted results. For example: Sometimes, no matter which of the members' shares I like, it will always render unlike on the first share of the page, although it will add the correct data to the database. Other times it will work correctly, but after refreshing the page, it will either render the like button (even though it was currently liked) or it will render the correct button, but it will raise an error when trying to "unlike" the share saying that there is no match to "destroy /opinions" which indicates it is not targeting specific shares.</p> <p>So to reiterate my my intentional question: How would I "successfully" render a like button (with the <code>class Opinion</code>) inside a member's UNIQUE share (<code>class Thought</code>) when it is inside a paginated list defined as <code>@share_items</code>.</p> <p>Sorry if I was confusing, first time posting here and I am new to Ruby. Thank you for any help you may give.</p>
 

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