我試圖爲我的應用程序添加一個簡單的星級評分系統,其中以this tutorial爲例。我有用戶,酒店和評分模型。依賴關係:Ruby on Rails:無法實現星級評分系統
(rating.rb)
belongs_to :user
belongs_to :hotel
(hotel.rb)&(user.rb)
has_many :ratings
而且,隨着酒店查看下面的代碼我得到這個錯誤:
NameError in Hotels#show
undefined local variable or method `user' for Class...
(與<%=的form_for線 ...)
酒店查看(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 %>
評分控制器:
def create
end
def update
end
def rating_params
params.require(:rating).permit(:stars)
end
遷移文件是:
create_table :ratings do |t|
t.integer :stars, :default => 0
t.references :store
t.references :user
end
你能給我們留下其他的錯誤信息嗎? – steel
當然, > NameError在酒店#顯示 顯示/home/mks/rails_projects/hotels/app/views/hotels/show.html.erb其中行#33上升: 未定義的局部變量或方法'用戶'爲# <#:0xb6314e74> –
在哪一行發生該錯誤? – Pavan