2010-11-19 8 views
1

ページの上部にすべてのメッセージを表示するのではなく、フォームフィールドのすぐ下に検証エラーメッセージを表示する方法を教えてください。RoR - フォームフィールドに別のエラーメッセージが表示される

私のRailsのバージョンは、私はフィールドID、タイトル、説明とテーブル名のカテゴリを持ってのRails 3.0.0

です。 私のモデルクラスは、あなたがformtasticに見たいと思うかもしれません

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

コントローラ

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 

と表示

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

答えて

2

私はおそらくこのようなことをします。このような新しい:

def new 
    @category = Category.new 
end 

をそして、代わりにこのようなform_tagののform_forを使用します。アクションで空のモデルを使用し

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

そしてアクションで:私はこれをしようと作成します。

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

このように、何らかの理由で作成が失敗した場合、コントローラはnewのテンプレートをレンダリングしますが、form_forヘルパーで失敗した@categoryオブジェクトを引き続き使用します。あなたが表示されたエラーメッセージを表示したいビューに次の

だから追加します(タイトル):そして、あなたはいつもの@ category.errors.onでモデルのエラーメッセージにアクセスすることができます

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

私が試しました'<%= form_for @category:action => '作成' do | f | %> '' RuntimeError in CategoriesController#new:属性を空白にすることはできません ' –

+0

申し訳ありませんが、私の側からの少しのタイプミスです。コンマが見つかりませんでした。もちろん、<%= form_for @category、:action => 'create' do | f | %>そして今、私はそれについて考えています:あなたはおそらくアクションハッシュも必要ないでしょう。試してみてください。<%= form_for @category do | f | %> – DanneManne

+0

私は既にこの構文を修正しましたが、実際には問題はform_forタグではなく、コントローラーの新しいメソッドの '@category = Category.new'ステートメントであり、この行のエラーが表示されなくなったとき。私はここで何が間違っている..? –

1

です。そこには素晴らしいものがたくさんあり、フィールドごとのエラーは非常にうまくいきます。

関連する問題