当partial被调用到索引中时,hidden_​​field_tag中的对象变为nil(但是,在单独的Show视图中工作)

时间:2012-05-10 03:40:20

标签: ruby-on-rails-3.1

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">&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 partial上的@tale.id无效。我无法弄清楚如何解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:-1)

好吧,我认为@tales的价值是零,所以请做以下事情让我知道结果会是什么 首先在您的PagesController中的home方法中添加 raise @ tales.Inspect _tale.html.erb中的第二个&lt;%= raise tale.inspect%&gt; 。 想检查故事的天气价值是否为空。