Reputation: 1765
I have the project organized in this way: User is the main resource, each user has one profile and each profile has one location as following:
resources :users do
resource :profile, :controller => "profiles" do
resource :location
end
Now I have to build a form for insert all the profile information but the location information as well (address etc). If I write the following code, it doesn't take care about the location.
<%= form_for(@profile, :url=>{:action=>'update'}, :html => {:multipart => true}) do |f| %>
Do someone have some suggestion for this situation ?
Tnx
Upvotes: 2
Views: 7997
Reputation: 7909
If you want to access different models within the same form you can use accepts_nested_attributes_for
. Here is a great screencast about the topic: http://railscasts.com/episodes/196-nested-model-form-part-1
Your code should something like this.
#profile.rb
accepts_nested_attributes_for :location
In your view:
<%= form_for(@profile, :url=>{:action=>'update'}, :html => {:multipart => true}) do |f| %>
<%= f.fields_for :location do |l| %>
//location fields here, for example:
<%=l.text_field :city %>
<% end %>
<% end %>
Upvotes: 5
Reputation: 46914
use :
form_for [@user, @profile, @location], :action => :update, :html => {:multipart => true}
Upvotes: 4