I have gotten a comment model to work based on railscast 238 but I would like to add voting and rating as well.
My current show for the article controller is:
def show
@article = Article.find(params[:id])
@commentable = @article
@comments = @commentable.comments
@comment = Comment.new
respond_to do |format|
format.html # show.html.erb
format.json { render json: @article }
end
end
I have tried replicating the same process and adding rateable but I do not know how to integrate it into the show action. I have considered creating a universal function from application controller and then trying to assign it in to multiple things(newb) in the show function but that seems over complicated.
I tried adding:
@rateable = @article
@ratings = @rateable.ratings
@rating = Rating.new
It did not work. But now I am thinking it might work if I assign it to
@ratings = Article.find(params[:id]}
@ratings = @rateable.ratings
@rating = Rating.new
Even if that does work, there must be cleaner way to do this.
Edit:
Error after correction from this line of code, which is identical to the working comment version.
<%= form_for [@rateable, @rating] do |f| %>
<% if @rating.errors.any? %>
<div class="error_messages">
<h2>Please correct the following errors.</h2>
<ul>
<% @rating.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.text_area :content, rows: 8 %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Okay, think I figured it out. Just change <%= f.submit %>
to:
<%= f.submit 'Submit', { controller: 'ratings', action: 'create', method: 'post' } %>
In your routes.rb
file you will also need put '/ratings/create' => 'ratings#create'
or simply resources :ratings
This will route your form submit to ratings#create
which I think is what you want.