Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, I found the problem!</p> <p>First, thanks to Dougui for making some good points that helped me on the way!</p> <p>Solution:</p> <p>I had to change the uniqueness validation from:</p> <pre><code>validates :tag, :uniqueness =&gt; {:scope =&gt; :post} </code></pre> <p>to:</p> <pre><code>validates :tag_id, :uniqueness =&gt; {:scope =&gt; :post_id} </code></pre> <p>You probably wonder why? It is explained here: <a href="http://thetenelements.blogspot.no/2011/08/undefined-method-text-for-nilnilclass.html" rel="nofollow">http://thetenelements.blogspot.no/2011/08/undefined-method-text-for-nilnilclass.html</a></p> <p>So here is my files when things are working:</p> <p><strong>like.rb</strong></p> <pre><code> validates :user_id, :uniqueness =&gt; {:scope =&gt; :post_id} belongs_to :user belongs_to :post </code></pre> <p><strong>likes_controller.rb</strong></p> <pre><code>def userLikePost current_user.likes.create(:post_id =&gt; params[:post_id]) end </code></pre> <p><strong>routes.rb</strong></p> <pre><code>match '/likes/:post_id', to: 'likes#userLikePost' </code></pre> <p><strong>pages.js.coffee</strong></p> <pre><code>$ -&gt; $(".like.btn.btn-mini").click (e) -&gt; if $(this).attr("class") == "like btn btn-mini" post_id = $(this).attr('id') $.post '/likes/' + post_id e.stopImmediatePropagation(); $(this).addClass("active") </code></pre> <p><strong>html button</strong></p> <pre><code>&lt;button class="like btn btn-mini" id="&lt;%= feed_item.id %&gt;"&gt;&lt;i class="icon-heart"&gt;&lt;/i&gt;&lt;/button&gt; </code></pre> <p>And in the user and post model I have <code>has_many: likes</code></p> <p>Hope this can help others :)</p>
    singulars
    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.
 

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