Ruby on Rails我有两个表单,如何将一个表单的提交标记移动到另一个表单内? [英] Ruby on Rails I have two Forms, How Do I move the submit tag of one form inside another form?

查看:273
本文介绍了Ruby on Rails我有两个表单,如何将一个表单的提交标记移动到另一个表单内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式:

  = 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'

如何将表单2中的表单1的提交标记移到表单2的下方标签显示在一个之前?



这不起作用:

  = 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'


解决方案

你只能有一种形式。
提交标签将同时返回form_for声明中定义的表单控制器操作。你需要在控制器动作中做的所有事情是检查提交参数(params [:commit] )作为按钮文本的值,并相应地在基于值。



因此,从部分2中删除form_for(也许可以在这里使用fields_for),将提交按钮移到form1,无论你想要它,然后检查提交参数散列为适当的值



  def update 
if params [:commit] =='Update form 1'
#do something
elsif params [:commit] =='更新表单2'
#还有其他的
else
#Rails an error - 你没有在你的表单提交按钮中设置正确的值
end
end

最好使用i18n作为按钮文本和控制器逻辑来测试按钮文本,然后您可以自由地将按钮文本更改为任何您想要的内容,而不会让您感到困惑。检查控制器

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

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

这篇关于Ruby on Rails我有两个表单,如何将一个表单的提交标记移动到另一个表单内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆