开发者

RoR - displaying separate error message s for form fields

开发者 https://www.devze.com 2023-01-26 09:14 出处:网络
How can i display the validation error messages just below the form field rather than showing all messages at the top of the page.

How can i display the validation error messages just below the form field rather than showing all messages at the top of the page.

My Rails version is Rails 3.0.0

I have a table name category with fields id, title and description. My model class is

class Category < ActiveRecord::Base
  validates_uniqueness_of :title, :message => "Title already exist"
  validates_presence_of :title, :description => "Cannot be blank"
end

Controller

class CategoriesController < ApplicationController

  def index
  end

  def new
  end

  def create
     @category = Category.new(params[:category])
     @category.created = Time.now
     @category.modified = Time.now
     respond_to do |format|
       if @category.save
         @category_last=Category.last
           format.html { redirect_to :controller => 'categories', :action => 'show', :id => @category_last.id }
  开发者_运维百科     else
          #format.html { redirect_to :controller => 'categories', :action => 'new' } 
        end
     end
  end

  def show
  end

  def edit
  end

end

and View

<div id="newCategory" class='page add'>
    <div class='screenTitle'>New Category</div>
    <div class='form_wrapper'>
        <%= form_tag :action=>'create' %>
            <div class='field_wrapper'>
                <div class='field_label'>
                    Title 
                </div>
                <div class='field_input'>
                    <%= text_area(:category, :description, :class=>'') %>
                </div>
                <div class='clearfix'>&nbsp;</div>
            </div>
            <div class='field_wrapper'>
                <div class='field_label'>
                    Title 
                </div>
                <div class='field_input'>
                    <%= text_field(:category, :title, :class=>'') %>
                </div>
                <div class='clearfix'>&nbsp;</div>
            </div>
            <div class='field_wrapper'>
                <div class='field_submit'>
                    <%= submit_tag "Submit", :type => "submit", :class => "submit" %>
                </div>
                <div class='clearfix'>&nbsp;</div>
            </div>
         </form>
    </div>
    <div class='actions'>
        <ul>
            <li><%= link_to 'List Categoris', root_url+'categories' %></li>
        </ul>
        <div class='clearfix'>&nbsp;</div>
    </div>
</div>


I would probably do something like this. Use an empty model in the action :new like this:

def new
  @category = Category.new
end

And then use form_for instead of form_tag like this:

<%= form_for @category, :action=>'create' do |f| %>
  <%= f.text_field(:title, :class=>'') %>

And then in the action :create I would try this:

if @category.save
  # redirect if you want to
else
  render :action => :new
end

That way, if the creation fails for some reason, the controller will render the template for :new but still use the failed @category object in the form_for helper. And you can always access the error messages of a model with @category.errors.on(:title)

So add the following to the view where you want the error message displayed:

<%= @category.errors.on(:title) unless @category.errors.on(:title).nil? %>


You might want to look into formtastic. Lots of awesome stuff in there and does error by field very nicely.

0

精彩评论

暂无评论...
验证码 换一张
取 消