2

Railsアプリケーションにsimple_formとtwitter boostrapを組み合わせて使用​​しています。そして、私は私を止める1つの問題を抱えています。

<%= f.input :title %>
<%= f.input :url %>
<%= f.input :tag_list, :label => 'Tags' %>
<%= f.input :type_id, :collection => @types, :label_method => :type_name, :value_method => :id, :include_blank => false %>
<%= f.input :description %>

それは私に素晴らしいフォームを作成します。ただし、検証が失敗すると、すべての入力フィールドにエラーが表示されます。しかし、選択フィールドではなく、選択フィールドからIDで満たされた通常の入力フィールドに変更されるだけで、理由はわかりません。

何か案は?

4

3 に答える 3

1

そうですね、あなたを助けるためにもっと情報が必要です。あなたのモデルを投稿していただけますか?またはそれが何であるか、そしてそれが何のためにあるのかを説明し@typesてください。

これは関連付けの基本的な単純です。

models/type.rb belongs_to :post

models/post.rb has_many :types

ビュー/投稿/_form.html.haml = f.association :types

タイプの「タイトル」または「名前」という名前の列があることを確認してください。

于 2012-08-13T08:59:32.793 に答える
1

レンダー アクションnewまたはeditの前に、メソッドcreateまたはupdateで@typesを設定する必要があります。

例えば

def new
  @your_object = YourObject.new
  @types = Type.all
  ....
end

def create
    @your_object = YourObject.new(your_object_params)

    respond_to do |format|
      if @your_object.save
        format.html { redirect_to ... }
        format.json { render action: 'show' ... }
      else
        ### ! init data for the form ! ###
        @types = Type.all
        format.html { render action: 'new' }
        format.json { render json: ... }
      end
    end
end
于 2014-03-16T23:19:12.630 に答える