Note that there are some explanatory texts on larger screens.

plurals
  1. PORails - undefined method `user_like' for #<#<Class:0x007fb1c80b0c30>:0x007fb1c81866f0>
    primarykey
    data
    text
    <p>I have an app that allows users to 'Like' and 'Unlike' the post, the 'Like' works well, however I'm having trouble for the function 'Unlike' and giving an error message for:</p> <pre><code>undefined method `user_like' for #&lt;#&lt;Class:0x007fb1c80b0c30&gt;:0x007fb1c81866f0&gt; </code></pre> <p>Could anyone help me out? Thanks!</p> <p>_like.html.erb</p> <pre><code> . . . &lt;% unless pin.liking_users.include? current_user %&gt; &lt;%= link_to likes_path(:pin_id =&gt; pin), class: "btn btn-mini", :method =&gt; :post, :remote =&gt; true do %&gt; &lt;i id="star_&lt;%= pin.id %&gt;" class="icon-star"&gt;&lt;/i&gt; &lt;% end %&gt; &lt;% else %&gt; &lt;%= link_to like_path(user_like(pin.id)), class: "btn btn-mini", :method =&gt; :delete, :remote =&gt; true do %&gt; &lt;i id="star_&lt;%= pin.id %&gt;" class="icon-star icon-red"&gt;&lt;/i&gt; &lt;% end %&gt; &lt;% end %&gt; . . . </code></pre> <p>likes_controller.rb:</p> <pre><code>class LikesController &lt; ApplicationController # GET /likes # GET /likes.json def index @likes = current_user.likes.map { |like| like.pin_id } @pins = @likes.map { |pin| Pin.find(pin) } end # POST /likes # POST /likes.json def create @pin = Pin.find(params[:pin_id]) @like = current_user.likes.build(:pin_id =&gt; params[:pin_id]) if !@pin.liking_users.include? current_user &amp;&amp; @like.save respond_to do |format| format.html { redirect_to root_url } format.js end else flash[:alert] = "Unable to like pin." redirect_to root_url end end # DELETE /likes/1 # DELETE /likes/1.json def destroy @like = Like.find(params[:id]) @pin = @like.pin if @like.destroy respond_to do |format| format.html { redirect_to root_url } format.js end end end end </code></pre> <p>models/pin.rb:</p> <pre><code> . . . belongs_to :user has_many :likes has_many :liking_users, :through =&gt; :likes, :source =&gt; :user . . . </code></pre> <p>models/user.rb:</p> <pre><code> . . . has_many :pins, :dependent =&gt; :destroy has_many :likes has_many :liked_pins, :through =&gt; :likes, :source =&gt; :pin . . . </code></pre> <p>models/like.rb:</p> <pre><code>class Like &lt; ActiveRecord::Base attr_accessible :created_at, :pin_id, :updated_at, :user_id belongs_to :user belongs_to :pin end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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