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.
I have a class Thought
(which are members' individual posts) that are paginated as @share_items
. I am including a like button that will allow members to like other members' posts (like Facebook). The like button has the class Opinion
.
My code thus far:
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
Partial _share.html.erb
:
<% if @share_items.any? %>
<ol class="member_shares">
<%= render 'shared/share_item', collection: @share_items %>
</ol>
<%= will_paginate @share_items %>
<% end %>
Which renders partial _share_item.html.erb
:
<li id="<%= share_item.id %>">
content...
<%= render 'thoughts/like_form' %>
...content
</li>
Everything goes smoothly until I try to render the form thoughts/like_form
inside paginated @share_items
(class Thought
). It has bizarre and unexplainable results (to me). Here are the codes leading up to the form.
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
And finally the form thoughts/like_form
:
<div id="like_form">
<% if registered_member.liked?(@thought) %>
<%= render 'thoughts/unlike' %>
<% else %>
<%= render 'thoughts/like' %>
<% end %>
</div>
which will render either thoughts/unlike
:
<%= form_for(registered_member.opinions.find_by_like_id(@thought),
html: { method: :delete },
remote: true) do |f| %>
<%= f.submit "Unlike", class: "btn btn-small btn-default" %>
<% end %>
or: thoughts/like
:
<%= form_for(registered_member.opinions.build(like_id: @thought.id),
remote: true) do |f| %>
<div><%= f.hidden_field :like_id %></div>
<%= f.submit "Like", class: "btn btn-small btn-primary" %>
<% end %>
Concluding with opinions/create.js.erb
:
$("#like_form<%= @thought.id %>").html("<%= escape_javascript(render('shared/unlike')) %>")
$("#likes").html('<%= @thought.likes.count %>')
and opinions/destroy.js.erb
:
$("#like_form<%= @thought.id %>").html("<%= escape_javascript(render('shared/like')) %>")
$("#likes").html('<%= @thought.likes.count %>')
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.
So to reiterate my my intentional question: How would I "successfully" render a like button (with the class Opinion
) inside a member's UNIQUE share (class Thought
) when it is inside a paginated list defined as @share_items
.
Sorry if I was confusing, first time posting here and I am new to Ruby. Thank you for any help you may give.
I have figured out a way to successfully render the like/unlike form for a member's individual post. While this is uncanny, space consuming and above all: lacks trend; it does work.
Instead of rendering like_form.html.erb
, I added the code directly to share_item.html.erb
and told the form not to look for @thought
but rather to look for share_item
(which is the individual item within the collection of @share_items
nested inside class Thought
)
thoughts/unlike.html.erb
now looks like:
<%= form_for(registered_member.opinions.find_by_like_id(share_item),
html: { method: :delete },
remote: true) do |f| %>
<%= f.submit "Unlike", class: "btn btn-small btn-default" %>
<% end %>
and thoughts/like.html.erb
<%= form_for(registered_member.opinions.build(like_id: share_item.id),
remote: true) do |f| %>
<div><%= f.hidden_field :like_id %></div>
<%= f.submit "Like", class: "btn btn-small btn-primary" %>
<% end %>
and I could omit <%= @thought.id %>
from the js so it looks like ("#like_form")
As I said, this junks up my partial having to add the code inside of it rather than rendering the form partial, but if I do the latter, I get the error that "share_item is an unknown or undefined local variable" While I have accomplished my initial goal getting the form to actually work, I am still seeking suggestions as to how I could render the form rather than adding it directly to my partial, without getting that error message.