Rails 3.1.3
我正在创建一个简单的网站,人们可以分享短篇小说,并可以在5星评级系统上对这些故事进行评分。星级评分系统就是问题所在。我可以让它在stories / show.html视图中正常工作,但不能在索引主页上工作。这是我的代码:
home.html.erb
<% content_for(:scripts) do %>
<%= javascript_include_tag 'rating_ballot'%>
<% end %>
<div id="talesFeedHome">
<p class="notice"><%= notice %></p>
<%= render @tales.sort_by { |tale| tale.created_at }.reverse %>
</div>
<p class="clear"> </p>
故事/ _tale.html.erb
<% if signed_in? %>
<div id="homeTales">
<ul>
<div id="taleShow">
<div id="controlPanel">
<li id="taleUserName"><%= tale.user.name %></li>
<li id="averageRating"> Your Rating:<br /><%= render "tales/stars" %></li>
</div>
<div id="taleDisplay">
<li><%= link_to(tale) do %>
<span><%= tale.title %> </span>
<span><%= tale.content %></span>
<% end %>
</li> <br />
</div>
</div>
</ul>
</div>
<% else %>
...
故事/ _stars.html.erb
<div id="starRating">
<%= form_for(rating_ballot, :remote => true, :html => { :class => 'rating_ballot' }) do |f| %>
<%= f.label("value_1", content_tag(:span, '1'), {:class=>"rating", :id=>"1"}) %>
<%= radio_button_tag("rating[value]", 1, current_user_rating == 1, :class => 'rating_button') %>
<%= f.label("value_2", content_tag(:span, '2'), {:class=>"rating", :id=>"2"}) %>
<%= radio_button_tag("rating[value]", 2, current_user_rating == 2, :class => 'rating_button') %>
<%= f.label("value_3", content_tag(:span, '3'), {:class=>"rating", :id=>"3"}) %>
<%= radio_button_tag("rating[value]", 3, current_user_rating == 3, :class => 'rating_button') %>
<%= f.label("value_4", content_tag(:span, '4'), {:class=>"rating", :id=>"4"}) %>
<%= radio_button_tag("rating[value]", 4, current_user_rating == 4, :class => 'rating_button') %>
<%= f.label("value_5", content_tag(:span, '5'), {:class=>"rating", :id=>"5"}) %>
<%= radio_button_tag("rating[value]", 5, current_user_rating == 5, :class => 'rating_button') %>
<%= hidden_field_tag(:tale_id, @tale.id) %>
<% end %>
</div>
此时,在隐藏字段标记中,我收到此错误:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
以下是3个相关控制器:
pages_controller.rb
class PagesController < ApplicationController
def home
@tales = Tale.all
end
end
tales_controller.rb
class TalesController < ApplicationController
respond_to :html, :js
def new
@tale = Tale.new
end
def show
@tale = Tale.find(params[:id])
end
def index
@tales = Tale.all
@tale = Tale.find(params[:id])
end
...
ratings_controller.rb
class RatingsController < ApplicationController
before_filter :authenticate_user!
respond_to :html, :js
def create
@tale = Tale.find_by_id(params[:tale_id])
@rating = Rating.new(params[:rating])
@rating.tale_id = @tale.id
@rating.user_id = current_user.id
if @rating.save
respond_to do |format|
format.html { redirect_to @tale, :notice => "Your rating has been saved" }
format.js
end
end
end
def update
@rating = current_user.ratings.find_by_tale_id(params[:tale_id])
@tale = @rating.tale
if @tale and @rating.update_attributes(params[:rating])
respond_to do |format|
format.html { redirect_to @tale, :notice => "Your rating has been updated" }
format.js
end
end
end
end
问题出在某处。不知何故,当在主页上呈现@tales时,这会使_stars partial上的@tale.id无效。我无法弄清楚如何解决这个问题。谢谢。
答案 0 :(得分:-1)