当将部分调用到索引中时,hidden_field_tag中的对象变为 nil(但在单个“显示”视图中工作)



Rails 3.1.3

我正在制作一个简单的网站,人们可以在其中分享短篇故事,并可以在 5 星评级系统上对这些故事进行评分。 星级评定系统是问题所在。 我可以让它在 stories/show.html 视图中正常工作,但在索引主页上则不能。 这是我的代码:

首页.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">&nbsp;</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上的@tale.id 无效。 我不知道如何解决这个问题。 谢谢。

好的,我认为你的@tales值为零,所以请做以下事情,让我知道结果会是什么首先把提高@tales。在页面控制器
中的主方法中检查第二个 <%= 在 _tale.html.erb 中提高 tale.inspect%>。想要检查故事的天气值是否为空。

最新更新