【问题标题】:Can't render validations errors in form partial无法在部分表单中呈现验证错误
【发布时间】:2013-10-18 16:26:05
【问题描述】:

我尝试在部分表单中呈现验证错误。

我有:

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

我该怎么办? 感谢您的帮助

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    您可以渲染到索引操作,而不是在其他部分重定向

    【讨论】:

    • 您也会在索引视图中看到对象错误。当我们在创建动作中保存对象并在错误出现时渲染到新对象时也是如此
    猜你喜欢
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 2016-08-02
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多