Ruby on rails 3.1 在索引中调用partial时,hidden_字段_标记中的对象变为nil(但在单个Show视图中有效)
轨道3.1.3 我正在制作一个简单的网站,人们可以在这里分享短篇故事,并可以在5星级评级系统上对这些故事进行评级。明星评级制度就是问题所在。我可以让它在stories/show.html视图中正常工作,但不能在索引主页上正常工作。这是我的密码: home.html.erbRuby on rails 3.1 在索引中调用partial时,hidden_字段_标记中的对象变为nil(但在单个Show视图中有效),ruby-on-rails-3.1,Ruby On Rails 3.1,轨道3.1.3 我正在制作一个简单的网站,人们可以在这里分享短篇故事,并可以在5星级评级系统上对这些故事进行评级。明星评级制度就是问题所在。我可以让它在stories/show.html视图中正常工作,但不能在索引主页上正常工作。这是我的密码: home.html.erb <% content_for(:scripts) do %> <%= javascript_include_tag 'rating_ballot'%> <% end %> <di
<% 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>
<% 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 %>
...
以下是3个相关控制器:
pages\u controller.rb
class PagesController < ApplicationController
def home
@tales = Tale.all
end
end
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
...
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
class PagesController
控制面板(U controller.rb)
class PagesController < ApplicationController
def home
@tales = Tale.all
end
end
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
...
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
class-TalesController
额定值控制器.rb
class PagesController < ApplicationController
def home
@tales = Tale.all
end
end
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
...
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
分级控制器“您的评级已保存”}
format.js
终止
终止
终止
def更新
@额定值=当前用户。额定值。按故事id查找故事(参数[:故事id])
@tale=@rating.tale
如果@tale和@rating.update_属性(参数[:rating])
回应待办事项|格式|
format.html{redirect_to@tale,:notice=>“您的评级已更新”}
format.js
终止
终止
终止
终止
问题就在这里的某个地方。不知何故,当在主页上呈现@tales时,_stars部分上的@tale.id无效。我想不出怎么解决这个问题。谢谢。好的,我认为你的@tales的价值为零,所以请做以下事情,让我知道结果如何 首先将提升@tales。在页面控制器的主页方法中检查 第二个在_tale.html.erb中。
想要检查tale的天气值是否为null。我得到了这个错误:在tales/\u stars.html.erb中尝试像tales中那样执行未定义的方法'Inspect'。\u tales部分中的同一行出现了相同的错误:“为nil调用id,这将错误地为4——如果您真的想要nil的id,请使用object\u id”