Good evening,
I'd like to set up a form with a single field (name), where the user inputs their name, and then they select one of three buttons.
我想设置一个带有单个字段(名称)的表单,用户输入其名称,然后选择三个按钮之一。
Each of these buttons should simultaneously set a particular attribute value, and submit the form.
这些按钮中的每一个都应同时设置特定的属性值,并提交表单。
A contrived example ("xxxx" is a button):
一个人为的例子(“xxxx”是一个按钮):
Name : _________
"Good" "Sad" "Tired"
This would save the user's name, and then yes/no/maybe to the "users_feeling" attribute. I don't want to have a radio box for the user feeling attribute and then a separate submit button.
这将保存用户的名称,然后是/ users_feeling属性。我不希望有一个用户感觉属性的单选框,然后是一个单独的提交按钮。
Any help you could provide would be appreciated!!
您可以提供的任何帮助将不胜感激!
1 个解决方案
#1
3
There are two ways how to do this in general.
一般来说,有两种方法可以做到这一点。
IMHO the better one is to simply add the attribute you want to set as a hidden field, and then write some javascript to set it to perhaps some data attribute on the button. Something like this:
恕我直言,更好的方法是简单地添加您想要设置为隐藏字段的属性,然后编写一些javascript以将其设置为按钮上的某些数据属性。像这样的东西:
$('.my_form button').click ->
$('#my_hidden_field').val ($ @).data 'attribute-name'
$('.my_form')[0].submit()
The other is that each submit button will pass its value
as a parameter when you submit the form. You can detect this in your rails controller like any other value. So if you have this HTML:
另一种是,当您提交表单时,每个提交按钮都会将其值作为参数传递。您可以像在任何其他值中一样在rails控制器中检测到这一点。所以如果你有这个HTML:
<input type=submit name=feeling value=Good />
<input type=submit name=feeling value=Sad />
<input type=submit name=feeling value=Tired />
Then in your controller params[:feeling]
should hold the value of the button clicked. Why I don't particularly like this solution is that the value
string is also what is displayed to the user which couples your controller with your view rather nastily. If in the future you'd like to localize your app this will likely bite you.
然后在你的控制器中,params [:feeling]应该保持点击按钮的值。为什么我不特别喜欢这个解决方案,因为值字符串也是显示给用户的内容,它将您的控制器与您的视图相结合。如果将来您想要本地化您的应用,这可能会让您感到困惑。
#1
3
There are two ways how to do this in general.
一般来说,有两种方法可以做到这一点。
IMHO the better one is to simply add the attribute you want to set as a hidden field, and then write some javascript to set it to perhaps some data attribute on the button. Something like this:
恕我直言,更好的方法是简单地添加您想要设置为隐藏字段的属性,然后编写一些javascript以将其设置为按钮上的某些数据属性。像这样的东西:
$('.my_form button').click ->
$('#my_hidden_field').val ($ @).data 'attribute-name'
$('.my_form')[0].submit()
The other is that each submit button will pass its value
as a parameter when you submit the form. You can detect this in your rails controller like any other value. So if you have this HTML:
另一种是,当您提交表单时,每个提交按钮都会将其值作为参数传递。您可以像在任何其他值中一样在rails控制器中检测到这一点。所以如果你有这个HTML:
<input type=submit name=feeling value=Good />
<input type=submit name=feeling value=Sad />
<input type=submit name=feeling value=Tired />
Then in your controller params[:feeling]
should hold the value of the button clicked. Why I don't particularly like this solution is that the value
string is also what is displayed to the user which couples your controller with your view rather nastily. If in the future you'd like to localize your app this will likely bite you.
然后在你的控制器中,params [:feeling]应该保持点击按钮的值。为什么我不特别喜欢这个解决方案,因为值字符串也是显示给用户的内容,它将您的控制器与您的视图相结合。如果将来您想要本地化您的应用,这可能会让您感到困惑。