Rails中的多页多模型表单 [英] Multi-page Multi-Model Form in Rails

查看:166
本文介绍了Rails中的多页多模型表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建使用多个模型的多页面表单。我有一个申请人,这个申请人有多个地址(一对多关系)。



我希望第一页包含有关申请人的信息,然后是之后的页面,以获得地址的表单(



这是我目前所拥有的:

applicant.rb

  has_many:addresses,:dependent => :销毁
accepting_attributes_for:地址

address.rb

  belongs_to:申请者

applicants_controller.rb :

  def new 
session [:applicant_params] || = {}

@申请人= Applicant.new(session [:applicant_params])
2.times do
@ addresses = @ applicant.addresses.build
end

session [:address_params ] = @ addresses.attributes

end

def create

session [:applicant_params] .deep_merge!(params [:applicant])if params [:Applicants]
session [:address_params] || = params [:address]
@applicant = Applicant.new(session [:applicant_params])
@ applicant.addresses.new(session [:address_params])



if params [:forward_button]或params [:back_button]


@ applicant.current_step = session [:applicant_step]

if params [:back_button]
@ap plicant.previous_step
else
@ applicant.next_step
end

session [:applicant_step] = @ applicant.current_step

rendernew

else

.....



<在新视图中:

 <%= form_for @applicant do | f | %GT; 

<%= render#{@applicant.current_step}_step,:f => f%>
< p><%= f.submitContinue,::name => forward_button除非@ applicant.last_step? %GT; < / p为H.
< p><%= f.submitBack,:name => back_button除非@ applicant.first_step? %GT;< / p为H.


<%end%>

@ applicant.current_step将为address_step或applicant_step,并且它们如下所示:



_applicant_step.html.erb

 < div class =field> 
<%= f.label:first_name%>< br />
<%= f.text_field:first_name,:width =>'10px',:size =>20,:maxlength =>20%>
< / div>
< div class =field>
<%= f.label:middle_name%>< br />
<%= f.text_field:middle_name%>
< / div>
....

_address_step.html.erb

 <%= f.fields_for:addresses do | u | %GT; 
< div class =field>
<%= u.label:address%>< br />
<%= u.text_area:address,:cols => 20,:rows => 5%>
< / div>
< div class =field>
<%= u.label:电话,电话号码%>< br />
<%= u.text_field:telephone%>
< / div>
...

就是这样。现在我的问题是如下,我想保存地址信息以及申请人信息。我只想说:@ applicant.addresses.new然后他们将被包含在申请人的会话信息中,但他们没有(当我得到地址页面时,没有字段!)。所以我创建了一个新的会话变量来保存地址信息。但我有一个问题。每次我从一个页面到另一个页面(在创建操作中),都会创建一个新的地址字段(及其所有属性)并将其添加到表单中。所以首先我有一个地址,然后我有2个,等等。我是否以这种错误的方式去做?我怎样才能拥有一个多页面的表单,以及多个模型(这是相关的),以及从一页到下一页的数据不会被删除。直到我最终到达最后一页,我可以提交(并保存)所有的模型..

我会很感激,如果有人可以帮助..
谢谢。

解决方案

你当然可以得到这个工作,但(取决于你需要的步骤数),它可能会变得非常复杂。



您是否考虑过在服务器端使用单个创建请求,并使用Javascript来分解在客户端形成多个步骤?看起来这可能会简单得多。您的Rails应用程序的行为更像一个标准的REST应用程序,但您可以将其分解,但您认为适合客户端。



我自己并没有使用它,但一个像这样的jQuery插件应该可以做到这一点:$ b​​ $ b http://thecodemine.org/


I'm trying to create a multi-page form that uses multiple models. I have an applicant and this applicant has more than one address (one to many relationship).

I would like the first page to contain information about the applicant, and then the page after that to have the form for the address(es)

This is what I have at the moment:

applicant.rb

has_many :addresses, :dependent => :destroy     
accepts_nested_attributes_for :addresses

address.rb

belongs_to :applicant

applicants_controller.rb:

def new
  session[:applicant_params] ||= {}

  @applicant = Applicant.new(session[:applicant_params])
  2.times do
    @addresses=@applicant.addresses.build
  end

  session[:address_params] = @addresses.attributes

end

def create

  session[:applicant_params].deep_merge!(params[:applicant]) if params[:applicant]
  session[:address_params] ||= params[:address]
  @applicant = Applicant.new( session[:applicant_params] )
  @applicant.addresses.new(session[:address_params])



  if params[:forward_button] or params[:back_button]


    @applicant.current_step = session[:applicant_step] 

  if params[:back_button]
    @applicant.previous_step  
  else
    @applicant.next_step
  end

  session[:applicant_step]=@applicant.current_step

  render "new"

else

.....

In the new view:

<%= form_for @applicant do |f| %>

  <%= render "#{@applicant.current_step}_step", :f => f %>
  <p><%= f.submit "Continue", :name => "forward_button" unless @applicant.last_step? %> </p>
  <p><%= f.submit "Back", :name => "back_button" unless @applicant.first_step? %></p>


<% end %>

@applicant.current_step will be either address_step or applicant_step, and these are below:

_applicant_step.html.erb

<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name ,:width=>'10px', :size=>"20", :maxlength=>"20" %>
</div>
<div class="field">
<%= f.label :middle_name %><br />
<%= f.text_field :middle_name %>
</div>
....

_address_step.html.erb

<%= f.fields_for :addresses do |u| %>
<div class="field">
    <%= u.label :address %><br />
    <%= u.text_area :address , :cols=> 20, :rows=>5%>
</div>
<div class="field">
    <%= u.label :telephone, "Telephone Number" %><br />
    <%=  u.text_field :telephone %>
</div>
...

And that's it. Now the problem I have is as follows, I want to save the address information as well as the applicant information. And I thought just by saying: @applicant.addresses.new then they will be included in the session information of the applicant, but they weren't (when I got the the address page, there were no fields!). So I created a new session variable to hold the address information. But I have a problem. Everytime I go from one page to the other (in the create action) a new address field (with all its attributes) is created and added to the form. So first I have one address, then I have 2 and so on. Am I going about this the wrong way? How can I have a multi-page form, with multiple models (that are related), and when going from one page to the next the data is not erased.. Until I eventually reach the last page where I could submit (and save) all the models..

I would be grateful if anyone could help.. Thank you.

解决方案

You can certainly get this working, but (depending on the number of steps you need), it could get pretty convoluted.

Have you considered using a single new and create request on the server side, and using Javascript to break up the form into multiple steps on the client side? It seems like this could be a lot simpler. Your Rails application will behave more like a standard REST application, and you can break this up however you see fit on the client side.

I have not used it myself, but a jQuery plugin such as this one should do the trick: http://thecodemine.org/

这篇关于Rails中的多页多模型表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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