简体   繁体   中英

Ruby on Rails I have two Forms, How Do I move the submit tag of one form inside another form?

I have two forms:

= form_for @form_one, :url => form_path do |f|
  = f.hidden_field :promotion_id
  = f.label :page_title, 'After Like: Page Title'
    = f.submit 'Update', :class => 'smBlueButton'

= render :partial => 'form_two'

How Do I move the Submit tag from form 1 below form 2, where form 2's submit tag is displayed before form one?

This does not work:

   = form_for @form_one, :url => form_path do |f|
      = f.hidden_field :promotion_id
      = f.label :page_title, 'After Like: Page Title'
    = render :partial => 'form_two'
      = f.submit 'Update', :class => 'smBlueButton'

It's fine to have 2 submit tags but you can only have one form. The submit tags will both post back to the forms controller action as defined in the form_for declaration. All you need to do in the controller action is check the commit param (params[:commit] ) for the value of the button text and act accordingly in a condition based on that value.

So remove the form_for from partial 2 (Perhaps a fields_for could be used here instead), move the submit button to form1 wherever you want it and check the commit params hash for the appropriate value

eg

def update
  if params[:commit] == 'Update form 1'
    #do something
  elsif params[:commit] == 'Update form 2'
    #do something else
  else
    #Rails an error - You have not set the right values in your form submit buttons
  end
end

Better to use i18n for the button text and the controller logic to test for the button text then you are free to change the button text to whatever you want without messing up you checks in your controller

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM