Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:00:27+00:00 2026-06-12T11:00:27+00:00

This is my first post here and to be honest, I don’t tend to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:00:30+00:00Added an answer on June 12, 2026 at 11:00 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first post here but I've been a frequent reader of various
This is my first post here after many visits. Hello! I am trying to
Hello all this is my first post here i have been working in an
This is my first post here so if I've been mistaken for my post
This is my first post here. I have been reading posts here since I
this is my first post here. I'm using an existing Flash widget but would
this is my first post here , I am trying to achive something in
Usless Background Info Hello, all. This is my first post here, but I often
This is my first post here and since I've seen many great answers I
This is my first post here, but I've using this site regularly to help

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.