我尝试在部分表单中呈现验证错误。
我有 :
resas/_form.html.erb
<%= form_for ([@resa, Resa.new]) do |f| %>
<% if @resa.errors.any? %>
<% @resa.errors.full_messages.each do |msg| %>
<h3><%= msg %></h3>
<% end %>
<% end %>
...
<div class="actions">
<%= f.submit %>
</div>
我通过另一个名为的部分在索引视图中显示此表单_agenda
:
resas/index.html.erb
<%= render "shared/agenda" %>
shared/_agenda.html.erb
<%= render "resas/form" %>
在控制器中:
def create
@resa = Resa.new(params[:resa])
respond_to do |format|
if @resa.save
format.html { redirect_to resas_path, notice: 'Resa was successfully created.' }
format.json { render json: @resa, status: :created, location: @resa }
else
format.html { redirect_to resas_path }
format.json { render json: @resa.errors, status: :unprocessable_entity }
end
end
end
我想重定向到索引操作,并呈现验证错误,但我不能。我有 :
NoMethodError in Resas#index
undefined method `errors' for nil:NilClass
我该怎么办?谢谢你的帮助