开发者

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

开发者 https://www.devze.com 2023-03-24 01:59 出处:网络
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\'

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开发者_StackOverflow社区 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

e.g.

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

0

精彩评论

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

关注公众号