开发者

Nested form problem

I have some code:

index.html.haml:

= form_tag 'search/index', :id => "index_form" do
  /* some fields there */
  = render :partial => 'geo_form', :object => @geo
  = submit_tag 'Search'

_geo_form.html.haml:

= form_tag 'search/save_geo', :id => "geo_form" do
  /* some fields there */
  = submit_tag 'Accept'

The p开发者_开发技巧roblem is: when I complete the second(nested) form and press the 'Accept' button, rails process the first form. How can I get a program to process the second form after clicking 'Accept' button.

Thanks!


you can use field_for method for nested forms.


The correct way to do this is to use fields_for. It doesn't actually create a new form tag, but arranges the field names so that the you can call @model.update_attributes(params[:model]) and both models will be updated (assuming they have some sort of ActiveRecord relationship like :has_many or :belongs_to)

The code will look something like this:

form_for @my_model1 do |f|
    f.text_field :name
    f.fields_for @my_nested_model do |nested|
        nested.text_field :start_date

You can find great detail in this railscast: http://railscasts.com/episodes/196-nested-model-form-part-1

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜