Reputation: 1576
After creating a Person associated with a specific Account, how do I redirect back to the Account page?
The account_id is passed to the CREATE Person action via a URL parameter as follows:
http://localhost:3000/people/new?account_id=1
Here is the code:
<h2>Account:
<%= Account.find_by_id(params[:account_id]).organizations.
primary.first.name %>
</h2>
<%= form_for @person do |f| %>
<%= f.hidden_field :account_id, :value => params[:account_id] %><br />
<%= f.label :first_name %><br />
<%= f.text_field :first_name %><br />
<%= f.label :last_name %><br />
<%= f.text_field :last_name %><br />
<%= f.label :email1 %><br />
<%= f.text_field :email1 %><br />
<%= f.label :home_phone %><br />
<%= f.text_field :home_phone %><br />
<%= f.submit "Add person" %>
<% end %>
class PeopleController < ApplicationController
def new
@person = Person.new
end
def create
@person = Person.new(params[:person])
if @person.save
flash[:success] = "Person added successfully"
redirect_to account_path(params[:account_id])
else
render 'new'
end
end
end
When I submit the above form I get the following error message:
Routing Error
No route matches {:action=>"destroy", :controller=>"accounts"}
Why is the redirect_to routing to the DESTROY action? I want to redirect via the SHOW action. Any help will be greatly appreciated.
Upvotes: 2
Views: 5273
Reputation: 34072
params[:account_id]
exists in the form, but when you pass it to create
you're sending it along in the person
hash, so you'd access it via params[:person][:account_id]
params[:account_id]
is nil
, hence the bad route. To be honest, I'm not sure why, but resource_path(nil)
ends up routing to destroy
instead of show
. In either case, it's a broken route without an id
parameter.
# so you *could* change it to:
redirect_to account_path(params[:person][:account_id])
# or simpler:
redirect_to account_path(@person.account_id)
# but what you probably *should* change it to is:
redirect_to @person.account
Rails will inherently understand this last option, ascertaining the path from the class of the record, and getting the id
from #to_param
Upvotes: 7
Reputation: 107738
I would not be pass this through using a hidden_field
. Instead, use nested resources:
resources :account do
resources :people
end
Then have an account object for the form:
<%= form_for [@account, @person] do |f| %>
...
<% end %>
This @account
object should be set up in the action that renders the form with a line like this:
@acccount = Account.find(params[:account_id])
Then when the form is submitted you'll have params[:account_id]
in that action without the ugly hidden_field
hack to get it there.
Yippee!
Upvotes: 1