I'm trying to add a simple Star Rating system for my app having taken this tutorial for an example. I have User, Hotel and Rating models. Dependencies are:
(rating.rb)
belongs_to :user
belongs_to :hotel
(hotel.rb) & (user.rb)
has_many :ratings
And with the following code in hotel view I get this error:
NameError in Hotels#show
undefined local variable or method `user' for Class...
(in the line with <%= form_for...)
Hotel view (show.html.erb):
<% form_id = "hotel_#{@hotel.id}_rating" %>
<% if signed_in? %> <!-- To avoid throwing an exception if no user is signed in -->
<% user_id = current_user.id %>
<% else %>
<% user_id = -1 %>
<% end %>
<%= form_for @hotel.ratings.find_or_create_by_user_id user.id,
:html => {:id => form_id,
:class => "star_rating_form"} do |f| %>
<%= f.hidden_field :hotel_id, :value => @hotel.id %>
<% if signed_in? %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<% end %>
<%= f.hidden_field :stars, :id => form_id + "_stars" %>
<% end %>
<% (1..5).each do |i| %>
<li class="rating_star" id="<%= form_id %>_<%= i %>" data-stars="<%= i %>" data-form-id="<%= form_id %>"></li>
<% end %>
Ratings controller is:
def create
end
def update
end
def rating_params
params.require(:rating).permit(:stars)
end
Migration file is:
create_table :ratings do |t|
t.integer :stars, :default => 0
t.references :store
t.references :user
end