我在Rails中显示错误时遇到了问题。 这里是我的控制器在Ruby on Rails中显示错误
def new
if current_user
@edible = Edible.new
else
flash[:notice] = "You need to be signed in for this action"
redirect_to root_path
end
end
def create
@edible = Edible.new(params[:edible])
if @edible.valid? && current_user.edibles.push(@edible)
if(params[:edible][:pickup] == "1")
respond_to do |format|
format.html { redirect_to new_user_edible_pick_up_adress(current_user.id, @edible.id) }
end
else
respond_to do |format|
format.html { redirect_to(user_edible_path(current_user.id, @edible.id),
:success => "Product saved")
}
end
end
else
respond_to do |format|
format.html { redirect_to(new_user_edible_path(current_user.id,
:alert => "Error happend"))
}
end
end
end
形式:
<%= nested_form_for @edible, :url => user_edibles_path,
:html => { :multipart => true, :class => "signin" } do |f| %>
<%= render 'shared/alerts', :object => @edible %>
#standart code further...
和部分:
<% if flash[:notice] %>
<p class="notice"><%= flash[:notice] %></p>
<% end %>
<% if flash[:error] %>
<p class="error">
<% if object %>
<%= flash[:error] %>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li> <%= msg %> </li>
<% end %>
</ul>
<% end %>
</p>
<% end %>
<% if flash[:alert] %>
<p class="alert"><%= flash[:alert] %></p>
<% end %>
我认为重定向到新的页面时@edible变量没有通过,所以路轨上行驶的对象每次都在新实例上创建错误。 我错过了一些明显的事情?
这个'partial'你有这个在'shared/alerts'目录里面吗? – David 2013-05-09 11:47:49
生成的HTML不包含'p.notice'?你确定你正在渲染正确的部分?你不应该需要将闪光灯传递给你的部分。 – fotanus 2013-05-09 11:55:05
部分位于正确的目录并且正在显示Flash消息,问题是<%= object.errors.full_messages.each ...%>不显示。 – DarkSun 2013-05-09 12:21:55