Rails 3:如何以嵌入形式显示错误消息? [英] Rails 3: How to display error messages in embedded form?

查看:52
本文介绍了Rails 3:如何以嵌入形式显示错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<预><代码>...<!-- 嵌入在boards/show.html.erb中的表单--><%= form_for([@board, @board.boardthreads.build]) 做 |f|%><div class="field"><%= f.label :title%><br/><%= f.text_field :title %>

<div class="field"><div class="actions"><%= f.submit %>

<%结束%>...类 Boardthread <ActiveRecord::Base归属地:用户归属地:董事会验证 :user, :presence =>真的验证 :board, :presence =>真的验证 :title, :presence =>真的结尾类 BoardthreadsController <应用控制器定义创建@board = Board.find(params[:board_id])@boardthread = @board.boardthreads.new(params[:boardthread])@boardthread.user = current_user@boardthread.saveredirect_to board_path(@board)结尾结尾

解决方案

这是因为当你失败时,你又在你的 Embedded_form 中构建了一个对象.您需要在表单中使用失败的对象.

在您的新操作中,您需要构建您的对象并在您的内嵌表单中使用它.在你创建的过程中你会使用它,因为它已经定义了

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

<div class="field"><div class="actions"><%= f.submit %>

<%结束%>

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.

...
<!-- 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

解决方案

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.

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

<%= 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 %>

这篇关于Rails 3:如何以嵌入形式显示错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆