Ruby-on-rails Rails 3:如何以嵌入形式显示错误消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3803180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:16:04  来源:igfitidea点击:

Rails 3: How to display error messages in embedded form?

ruby-on-rails

提问by PlankTon

I'm new to rails & trying to set up my first embedded form. The form itself works, but I can't determine how to send validation error messages to the view. I assumed f.object.errors would provide access, but while the method is said to exist, f.object.errors.count always returns 0, and f.object.errors.any? returns false. Apart from not showing the actual error messages, the form is working as expected - that is, failing to insert invalid data and returning to the form which failed validation. Model, controller & view listed below - any help much appreciated.

我是 Rails 的新手,正在尝试设置我的第一个嵌入式表单。表单本身有效,但我无法确定如何将验证错误消息发送到视图。我假设 f.object.errors 会提供访问权限,但是虽然据说该方法存在,但 f.object.errors.count 总是返回 0,而 f.object.errors.any? 返回假。除了不显示实际的错误消息外,表单按预期工作 - 即未能插入无效数据并返回到验证失败的表单。下面列出了模型、控制器和视图 - 非常感谢任何帮助。

...
<!-- Form embedded in boards/show.html.erb -->
<%= form_for([@board, @board.boardthreads.build]) do |f| %> 
    <div class="field">  
        <%= f.label :title %><br />  
        <%= f.text_field :title %>  
    </div>  
    <div class="field">    
        <div class="actions">  <%= f.submit %>  </div> 
    </div>
<% end %>
...



class Boardthread < ActiveRecord::Base
  belongs_to :user
  belongs_to :board

  validates :user, :presence => true
  validates :board, :presence => true
  validates :title, :presence => true
end


class BoardthreadsController < ApplicationController
    def create

        @board = Board.find(params[:board_id])
        @boardthread = @board.boardthreads.new(params[:boardthread])  
        @boardthread.user = current_user
        @boardthread.save
        redirect_to board_path(@board) 

    end
end

回答by shingara

It's because when you failed, you build again an object in your embedded_form. You need use the object with failure in your form.

这是因为当你失败时,你在你的embedded_form 中再次构建了一个对象。您需要在表单中使用失败的对象。

In your new action you need build your object and use it on your embedded_form. And during your create you use it because it's already define

在您的新操作中,您需要构建您的对象并在您的embedded_form 上使用它。在你创建的过程中你会使用它,因为它已经定义了

<%= form_for([@board, @boardthread]) do |f| %>
    <% @boardthread.errors.full_messages.each do |msg| %>
      <p><%= msg %></p>
    <% end %>
    <div class="field">  
        <%= f.label :title %><br />  
        <%= f.text_field :title %>  
    </div>  
    <div class="field">    
        <div class="actions">  <%= f.submit %>  </div> 
    </div>
<% end %>

回答by Yannis

In addition to shingara answer: You may also need to add the code to display the errors in your form, someting like

除了 shingara 答案:您可能还需要添加代码以显示表单中的错误,例如

<ul>
  <%-  @boardthread.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <%- end %>
</ul>`