여러 모델을 사용하는 다중 페이지 양식을 만들려고합니다. 나는 신청자가 있고이 신청자는 하나 이상의 주소 (일대 다 관계)를가집니다.여러 페이지 다중 모델 양식의 레일
:
나는 주소 (들)에 대한 양식을 가지고 그 이후 신청자에 대한 정보를 포함하는 첫 번째 페이지, 다음 페이지를 원하는
이것은 내가 지금 무엇을 가지고 applicant.rb
has_many :addresses, :dependent => :destroy
accepts_nested_attributes_for :addresses
address.rb
belongs_to :applicant
을 덮은 nts_controller.rb : applicant.current_step @
<%= 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 %>
중 하나 address_step 또는 applicant_step 될 것이며, 이들은 다음과 같습니다 : 새로운 관점에서
def new
session[:applicant_params] ||= {}
@applicant = Applicant.new(session[:applicant_params])
2.times do
@[email protected]
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][email protected]_step
render "new"
else
.....
_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>
...
그리고 그게 전부입니다. 이제 내가 가진 문제는 다음과 같습니다. 주소 정보와 신청자 정보를 저장하려고합니다. 그리고 나는 단지 다음과 같이 말한 것으로 생각했습니다 : @ applicant.addresses.new 그들은 신청자의 세션 정보에 포함될 것입니다. 그러나 그들은 (주소 페이지를 가지고있을 때, 필드가 없습니다!). 그래서 주소 정보를 저장할 새 세션 변수를 만들었습니다. 그러나 나는 문제가있다. 한 페이지에서 다른 페이지로 이동할 때마다 (작성 액션에서) 새 주소 필드 (모든 속성 포함)가 작성되어 양식에 추가됩니다. 그래서 처음에는 하나의 주소를 가지고 있고, 그 다음에는 2를 가지고 있습니다. 내가 잘못된 방향으로가는거야? 어떻게 여러 페이지가있는 양식을 가질 수 있습니까? 여러 모델이 관련되어 있습니다. 한 페이지에서 다음 페이지로 이동할 때 데이터가 지워지지 않습니다. 결국 마지막 페이지를 통해 제출할 수 있습니다 (저장) 모든 모델 ..
누군가가 도울 수 있으면 감사 할 것입니다. 감사합니다.
은 링크를 주셔서 감사합니다. 내 기존 코드에서 작동하는 솔루션을 찾고 싶었는데, 가까운 곳에 다가 가고 있다고 느꼈기 때문에. 누군가 어떻게 든 고칠 수 있도록 도와 주면 좋을 것 같습니다. .. 다시 한 번 감사드립니다. – user1069624