简体   繁体   English

Ruby on Rails我有两种形式,如何在一种形式内移动一种形式的Submit标签?

[英]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? 如何从表单2下方的表单1移动提交标签,在表单1之前显示表单2的提交标签?

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. 可以有2个提交标签,但您只能有一种形式。 The submit tags will both post back to the forms controller action as defined in the form_for declaration. 提交标签都将回发到form_for声明中定义的表单控制器操作。 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. 在控制器操作中,您需要做的只是检查按钮文本值的commit参数(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哈希值是否合适

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 最好将i18n用于按钮文本,并使用控制器逻辑来测试按钮文本,然后可以随意将按钮文本更改为所需的内容,而不会弄乱控制器中的检查内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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