【问题标题】:Rails: Form Validation Errors Are Not Showing (with Formtastic and Bootstrap-Sass)Rails:未显示表单验证错误(使用 Formtastic 和 Bootstrap-Sass)
【发布时间】:2012-11-21 04:03:40
【问题描述】:

我正在使用 Bootstrap-Sass 和 Formstatic。我认为应该在带有 Formstatic 的字段旁边自动显示一条错误消息,如下图所示:
(来源:asciicasts.com

但即使用户输入了无效输入,我的应用程序也不会显示错误消息。这似乎是一个简单的问题,但我无法弄清楚背后的原因。

后控制器

# POST /posts
# POST /posts.json
def create
  @post = Post.new(params[:post])
  @post.view = 0
  @post.like = 0 
  @post.hate = 0
  respond_to do |format| 
    if @post.save
      @posts = Post.paginate(:page => params[:page], order: 'like desc', per_page: 10) 
      format.html { redirect_to posts_path }
      format.json { render json: @post, status: :created, location: @post }
    else
      format.html { render action: "new" }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end
end

后模型

  validates :name,  :presence => true
  validates :content, :presence => true,
                      :length => { :minimum => 10, :maximum => 300}

_form(帖子)

<% @post = Post.new %>
<%= semantic_form_for @post do |f| %>
<%= f.semantic_errors :name %>
<%= f.inputs do %>
     <%= f.input :name, :label => 'name' %>
     <%= f.input :content, :label => 'body' %>
<% end %>
<%= f.actions do %>
    <%= f.action :submit, :button_html => { :class => "btn btn-primary" }, :as => :button  %>
    <%= f.action :cancel, :as => :link %>
<% end %>

在 PostController 中,我尝试删除以下两行

    #format.html { render action: "new" }
    #format.json { render json: @post.errors, status: :unprocessable_entity }

并添加

render @post.errors

然后,我得到了

@messages={:name=>["can't be blank"], :content=>["can't be blank", "is too short (minimum is 10 characters)"]}>

所以我认为问题可能是我渲染 json 的方式是错误的。有人可以帮我解决一下吗?

【问题讨论】:

    标签: ruby-on-rails forms validation formtastic


    【解决方案1】:

    Rails 有自己的验证错误渲染,与 Bootstrap 使用的 HTML 结构或 CSS 结构不匹配。

    要解决这个问题,您只需添加以查看您自己的代码块以了解输出错误。

    <% if @posts and @posts.errors and @posts.errors.count > 0 %>
     <div class="alert alert-danger">
       <a class="close" data-dismiss="alert">&times;<a>
       <strong><%= pluralize(@posts.errors.count,"error") %> validation problems found.</strong>
       <ul>
       <% @posts.errors.full_messages.each do |error| %>
         <li><%= error %></li>
       <% end %>
       </ul>
     </div>
    

    或者您可以将此块移动到部分模板以避免代码重复。

    <% if resource and resource.errors and resource.errors.count > 0 %>
      <div class="alert alert-danger">
        <a class="close" data-dismiss="alert">&times;<a>
        <strong><%= pluralize(resource.errors.count,"error") %> validation problems found.</strong>
        <ul>
        <% resource.errors.full_messages.each do |error| %>
          <li><%= error %></li>
        <% end %>
        </ul>
      </div>
    <% end %>
    

    并将@posts 传递给参数

    <%= render "shared/validation_errors", :resource => @posts %>
    

    您可以在此处找到有关此问题的更多信息 http://fizzylogic.nl/2013/12/22/temp-slug-54/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-17
      • 2014-07-25
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 2014-02-20
      相关资源
      最近更新 更多