2012-01-23 5 views
0

私はフォームに2つのセクションがあり、その視認性を切り替えるボタンがあります。送信ボタンが隠しボタンからパラメータを送信するのを制限する方法はありますか?残念ながら、名前や番号のないコースを作成していて、collection_selectを使用すると既存のコースを選択していません。Rails 3.2 2つのフォーム、1つの隠し

プロジェクト/ new.html.haml

= form_for [@user, @project] do |f| 

    # This part of the form is mostly shown to the user, but is failing to work correctly 
    = f.collection_select :course_id, @courses, :id, :name, { prompt: true } 

    # This part of the form is typically hidden, javascript reveals it. 
    .hidden 
    = f.fields_for :course do |builder| 
     = builder.text_field :name, class: 'large', placeholder: 'Ex: Calculus I' 
     = builder.label :number, 'Number' 
     = builder.text_field :number, class: 'new_project_course_number', placeholder: 'Ex: MATH-101' 
     = builder.hidden_field :user_id, value: current_user.id 

project.rb

belongs_to :user 
belongs_to :course 

attr_accessible :course_id, :course_attributes 
accepts_nested_attributes_for :course 

course.rb

belongs_to :user 
has_many :projects 

user.rb

has_many :projects 
has_many :courses 

は、私が誤ってすべての重要な情報をオフに残していた場合、私に知らせてください。

答えて

1

ネストされた属性セットのためにreject_if paramを探している可能性があります。 例:

accepts_nested_attributes_for : course, :reject_if => proc { |attributes| 
    attributes['name'].blank? 
} 

またはこのようなものです。提出されたフォームをそのまま残しておきますが、名前がプリセットされている場合はネストされたコースオブジェクトのみが作成されます(あなたの場合はプレースホルダがいくつかあるので、ここで別のチェックを使用するかもしれません)

関連する問題