Note that there are some explanatory texts on larger screens.

plurals
  1. POLike/Unlike Without Ajax or jQuery on Rails
    primarykey
    data
    text
    <p>I'm trying to implement a simple like/unlike function. All the examples I have seen here seem to apply to ajax or jquery. I.m still a beginner and I don't fully understand either yet, I just want a simple solution.</p> <p>My idea is, I have books and I have users. Users can like books. So I have created a many-to-many association through a Like model. The Like model has a corresponding database with book_id and user_id columns. Thus: </p> <pre><code>class Book &lt; ActiveRecord::Base has_many :likes has_many :users, through: :likes class User &lt; ActiveRecord::Base has_many :likes has_many :books, through: :likes class Like &lt; ActiveRecord::Base belongs_to :book belongs_to :user end </code></pre> <p>unfortunately, this is as far as my understanding goes. I do not know how to utilize this relationship to create likes and associate them with the appropriate book and user.</p> <p>My idea is to present the user a 'like' button if he has not liked the book, or unlike if he hasn't. So basically, I want to have something this simple:</p> <pre><code>&lt;% if user likes? %&gt; &lt;div class="unlike_button"&gt;&lt;%= link_to "Unlike", '#' %&gt;&lt;/div&gt; &lt;% else %&gt; &lt;div class="like_button"&gt;&lt;%= link_to "Like", '#' %&gt;&lt;/div&gt; &lt;% end %&gt; </code></pre> <p>I used the # stub because I have no idea what route it should use. But whatever the solution is, I would like it to redirect to the same page with a flash notice saying "liked" or "Unlike". I already have div background images for the like_button and unlike_button classes, which is why I implement them as image links above.</p> <p>Any sort of guidance or help, would be deeply appreciated. Thanks</p> <p><em>UPDATE</em></p> <p>I'm following Bennick's guidance here below but I', still stuck in the rails console. I figure there's no point moving forward if I'm getting errors in the console.</p> <p>As suggested, I tried this in the console:</p> <p>As I already have users I did <code>user = User.find(1)</code>and <code>book = Book.find(1)</code></p> <p>But on the next line <code>like = Like.create(:user =&gt; user, :book =&gt; book)</code>the returned a mass assign error. <code>Can't mass-assign protected attributes: book, user</code> I figured maybe this would help <code>attr_accessible :book_id, :user_id</code> in the like model but I still get errors. Am I missing something?</p> <p><em>SOLVED</em></p> <p>I finally got it to work! Using <code>like = Like.create(:user =&gt; user.id, :book =&gt; book.id)</code>. </p>
    singulars
    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