Frustrated With Nesting in Ruby on Rails-Collection of common programming errors

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:


    
    
    

    Which renders partial _share_item.html.erb:

  1. content... ...content
  2.  
    

    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:

    
    
    
    
    
    
    
    

    which will render either thoughts/unlike:

    
    
    
    

    or: thoughts/like:

    
    
    
    
    

    Concluding with opinions/create.js.erb:

    $("#like_form").html("")
    $("#likes").html('')
    

    and opinions/destroy.js.erb:

    $("#like_form").html("")
    $("#likes").html('')
    

    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.

    1. 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:

      
      
      
      

      and thoughts/like.html.erb

      
      
      
      
      

      and I could omit 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.

    2. When you have nested objects, you should declare your form like this:

      form_for [@member, @opinion]
      

    Originally posted 2013-11-09 23:11:45.