문제

Unable to display validation errors using the gem 'bootstrap_forms', '~> 2.0.0' (http://github.com/potenza/bootstrap_form).

models/client.rb

class Client < ActiveRecord::Base
  attr_accessible :name
  validates :name, :presence => true
end

views/clients/new.html.haml

= bootstrap_form_for @client do |f|
  = f.object.errors.messages
  %h1 Create New Client
  .row-fluid
    .span3= f.text_field :name, :class => 'span50'
  = f.submit

When submitting the above form code, f.object.class is 'Client', and f.object.messages is an empty hash.

Switching this code to use rails built in form builder allows me to see the validation errors:

views/clients/new.html.haml

= form_for @client do |f|
  = f.object.errors.messages
  %h1 Create New Client
  .row-fluid
    .span3= f.text_field :name, :class => 'span50'
  = f.submit

Using this, f.object.messages is {:name=>["can't be blank"]}.

Does this make any sense? I cannot understand why f.object.errors.messages is empty.

도움이 되었습니까?

해결책

Try this:

  f.errors.full_messages
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top