我无法在 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 变量,因此 Rails 每次都在新实例上运行 object.errors。我错过了一些明显的东西吗?