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?
如何从表单2下面的表单1移动提交标记,表单2的提交标记在表单1之前显示?
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'
1 个解决方案
#1
3
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.
有2个提交标签很好,但你只能有一个表格。提交标记将回发到form_for声明中定义的表单控制器操作。您需要在控制器操作中执行的操作是检查提交参数(params [:commit])以获取按钮文本的值,并在基于该值的条件中相应地执行操作。
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
所以从部分2中删除form_for(也许可以在这里使用fields_for),将提交按钮移动到form1,无论你想要它,并检查commit params hash是否适当的值
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
最好使用i18n作为按钮文本和控制器逻辑来测试按钮文本然后你可以*地将按钮文本更改为你想要的任何内容而不会弄乱你在控制器中的检查
#1
3
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.
有2个提交标签很好,但你只能有一个表格。提交标记将回发到form_for声明中定义的表单控制器操作。您需要在控制器操作中执行的操作是检查提交参数(params [:commit])以获取按钮文本的值,并在基于该值的条件中相应地执行操作。
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
所以从部分2中删除form_for(也许可以在这里使用fields_for),将提交按钮移动到form1,无论你想要它,并检查commit params hash是否适当的值
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
最好使用i18n作为按钮文本和控制器逻辑来测试按钮文本然后你可以*地将按钮文本更改为你想要的任何内容而不会弄乱你在控制器中的检查