I have two forms in my rails app. They both exist in separate tabs and I when I submit one form I want the data in the other form to be saved as well. How should I do that? Or Is there a better way of doing this instead of using two separate forms? Is there a better way to spread a long form into multiple tabs and when I press submit all the data from all the tabs should reach my action. Thanks.
我的rails应用程序中有两个表单。它们都存在于单独的选项卡中,当我提交一个表单时,我希望保存另一个表单中的数据。我该怎么做?或者有更好的方法来做这个而不是使用两个单独的形式?有没有更好的方法将长格式分散到多个标签中,当我按下提交时,所有标签中的所有数据都应该达到我的操作。谢谢。
3 个解决方案
#1
You can expand a single form to cover all elements of both forms.
您可以展开单个表单以涵盖两个表单的所有元素。
This is perfectly ok, as long as the form tags fit validly into the X/HTML.
只要表单标签有效地适合X / HTML,这就完全可以了。
<form action='action1'>
<!-- All elements from both forms, plus tabs, etc. -->
</form>
The only thing to consider is if there will ever be another form inside that area that will need to go to another action. For example, if you add a third tab, in between the other two, that will have a different action.
唯一要考虑的是,如果该区域内还有另一种形式需要进行另一种行动。例如,如果在其他两个选项卡之间添加第三个选项卡,则会有不同的操作。
You don't want to end up like:
你不想最终像:
<form action='action1'>
<!-- elements from the combined forms -->
<form action='action2'>
<!-- elements for a totally different form, not valid inside another form -->
</form>
<!-- more elements from the combined forms -->
</form>
In that case, it would be better to consolidate the two forms at submit time using JavaScript.
在这种情况下,最好使用JavaScript在提交时合并两个表单。
jQuery (jquery.com) would make this pretty easy. For example, you could serialize both forms, and then concatenate them and send the result to the server via post or get.
jQuery(jquery.com)会让这很容易。例如,您可以序列化两个表单,然后连接它们并通过post或get将结果发送到服务器。
See http://docs.jquery.com/Ajax/serialize.
There might be better ways to do this, but I can't think of any off the top of my head.
可能有更好的方法来做到这一点,但我无法想到任何一个问题。
#2
I can't test if the concept work right now, but I believe that you could use jQuery to achieve that, its submit function will intercept all form submissions on any tab, something along these lines
我无法测试这个概念现在是否正常工作,但我相信你可以使用jQuery来实现它,它的提交函数将拦截任何标签上的所有表单提交,这些都是这些
$("form").submit(function(event){
event.preventDefault();
//serialize forms here and submit using jquery post
});
#3
You should look into the fields_for method. You can do something like this:
您应该查看fields_for方法。你可以这样做:
<% form_for @house do |f| %>
<%= f.text_field :square_feet %>
<% fields_for @listing do |s| %>
<%= s.hidden_field :id %>
<%= s.text_field :asking_price %>
<% end %>
<%= f.submit %>
<% end %>
And then in your controller you'd do something like this:
然后在你的控制器中你会做这样的事情:
house = House.find(params[:id])
house.update_attributes(params[:house])
listing = Listing.find(params[:listing][:id])
listing.update_attributes(params[:listing])
That's for an update, but you can do whatever you want in there.
那是为了更新,但你可以在那里做任何你想做的事。
#1
You can expand a single form to cover all elements of both forms.
您可以展开单个表单以涵盖两个表单的所有元素。
This is perfectly ok, as long as the form tags fit validly into the X/HTML.
只要表单标签有效地适合X / HTML,这就完全可以了。
<form action='action1'>
<!-- All elements from both forms, plus tabs, etc. -->
</form>
The only thing to consider is if there will ever be another form inside that area that will need to go to another action. For example, if you add a third tab, in between the other two, that will have a different action.
唯一要考虑的是,如果该区域内还有另一种形式需要进行另一种行动。例如,如果在其他两个选项卡之间添加第三个选项卡,则会有不同的操作。
You don't want to end up like:
你不想最终像:
<form action='action1'>
<!-- elements from the combined forms -->
<form action='action2'>
<!-- elements for a totally different form, not valid inside another form -->
</form>
<!-- more elements from the combined forms -->
</form>
In that case, it would be better to consolidate the two forms at submit time using JavaScript.
在这种情况下,最好使用JavaScript在提交时合并两个表单。
jQuery (jquery.com) would make this pretty easy. For example, you could serialize both forms, and then concatenate them and send the result to the server via post or get.
jQuery(jquery.com)会让这很容易。例如,您可以序列化两个表单,然后连接它们并通过post或get将结果发送到服务器。
See http://docs.jquery.com/Ajax/serialize.
There might be better ways to do this, but I can't think of any off the top of my head.
可能有更好的方法来做到这一点,但我无法想到任何一个问题。
#2
I can't test if the concept work right now, but I believe that you could use jQuery to achieve that, its submit function will intercept all form submissions on any tab, something along these lines
我无法测试这个概念现在是否正常工作,但我相信你可以使用jQuery来实现它,它的提交函数将拦截任何标签上的所有表单提交,这些都是这些
$("form").submit(function(event){
event.preventDefault();
//serialize forms here and submit using jquery post
});
#3
You should look into the fields_for method. You can do something like this:
您应该查看fields_for方法。你可以这样做:
<% form_for @house do |f| %>
<%= f.text_field :square_feet %>
<% fields_for @listing do |s| %>
<%= s.hidden_field :id %>
<%= s.text_field :asking_price %>
<% end %>
<%= f.submit %>
<% end %>
And then in your controller you'd do something like this:
然后在你的控制器中你会做这样的事情:
house = House.find(params[:id])
house.update_attributes(params[:house])
listing = Listing.find(params[:listing][:id])
listing.update_attributes(params[:listing])
That's for an update, but you can do whatever you want in there.
那是为了更新,但你可以在那里做任何你想做的事。