开发者

How do I add addresses to a person from the add person form?

开发者 https://www.devze.com 2023-04-09 00:32 出处:网络
I have a (rails 3/jquery) application where a user comes across a new person form. In the form, one of the things he can do is to add addresses as he is constructing the person. I want the addresses t

I have a (rails 3/jquery) application where a user comes across a new person form. In the form, one of the things he can do is to add addresses as he is constructing the person. I want the addresses to be added, and displayed (either through Ajax, or on the page itself), but the addresses should be committed only once the person is saved (if the user cancels the person creation, the addresses should also not be saved). The person form has fields for a person name, and a button to add addresses. On clicking on add address, a dialog pops up to enter addresses. As each address is added, it is shown inline with the form.

What is the best way to do this? I am currently popu开发者_开发技巧lating a div within the person using jquery to enter addresses: <%= form_tag ... do %> <%= text_field_tag :person_name%> <%= link_to 'Add address', nil, :id => 'add_address_link', :remote => true%>

<%= submit_tag%> <%end%> $('#add_address_link').click(function(){ $('#addresses').append(...); });

I could handle the submit, and read the address elements into a series of hidden fields (not shown here), or use ajax instead of direct jquery click handler, and populate the hidden fields in an erb. Not sure what is the best way to achieve this?

Thanks Anand


Rails 3 supports nested forms with the fields_for form helper. Check out http://guides.rubyonrails.org/form_helpers.html#binding-a-form-to-an-object.

You should show the model associations in your question, but I'll assume you have:

class Person < ActiveRecord::Base
  has_many :addresses
end

class Address < ActiveRecord::Base
  belongs_to :person
end

Now, you want to dynamically add duplicate fields for each address. While I don't think ryanb's nested_form gem is necessary, you should look at his railscast http://railscasts.com/episodes/197-nested-model-form-part-2

The railscast demonstrates dynamically adding a row to a table where each row is a nested form using fields_for

There is a companion project on github that includes the code to do that.

Finally, you want to adapt ryanb's code to use hidden fields instead of table rows. I'm actually in the process of doing exactly this.

Another option is to restructure your form workflow. I had another use case which sounds even more like what you've described and the easiest thing for me to do was, using the same _form partial for Person, only show the child objects (Address in your case) if there are any. Naturally, on a "new" action, there aren't (unless you have done something in a Person callback). Then, on the Person "create" action, rather than redirect to a Person index page (typical), redirect back to the edit_person_path. Then, you can use remote/ajax to add addresses to the person, since the person will exist.


What does your database look like? If Persons and Addresses are separate tables, I would recommend using Ryanb's nested_form gem. The gem uses jQuery and makes adding dynamic nested forms very easy and fast. The gem supports has_many relationships, so you could allow a person to have multiple addresses.

https://github.com/ryanb/nested_form


I am assuming that you have a separate model - Address. In this case, just pass the params used for creating a new user into the address controller, and in the create action for the Address controller, call @address.save only if a Person with the params exists (if Person.find is successful).

0

精彩评论

暂无评论...
验证码 换一张
取 消