Quick, probably n00bish question, but I can't seem to figure it out.
I am developing a contact management application, and while I am using REST, I have also defined a custom route to make it more understandable to the end user:
map.resources :contacts
map.details 'details/:id', :controller => 'contacts', :action => 'edit' # CRM-type apps allow editing contact details on the viewing page, instead of a separate show/edit page
Anyways, the issue is when a record contains invalid data. GIven the following action:
def update
@contact = Contact.find(params[:id])
if @contact.update_attributes(params[:contact])
flash[:notice] = "Contact information was updated"
redirect_to contacts_url
else
render :action => "edit"
end
end
the address bar displays http://mysite/contacts/1, which results in a Missing Template if the user accidently hits enter (as I don't have a Show action, Edit takes it's place). Is there something I need to add to the render method to make sure that my route is kept? I don't want the user to see contacts/1 but details/1. I tried changing it to redirect_to details_path(@contact), but then no error messages are displayed when the page reloads.
What does your form look like? For that matter, look in the log and see
if you are submitting to contacts or details. I suspect you are
submitting to details.
But, my other guess / suggestion is to add :controller => "details" to
your render line.
I'm sure I'm just forgetting to add a line somewhere to render the
named route instead of the regular route, I'm just not sure where said
line should go
This is why you are posting to the contact path instead of the details
path. It is just taking all the defaults and assumptions but you want
something different. (In particular, where to post to).
You need to change the form_for @contact to something else. Here is a
sample from the documentations:
Notice that they specify the type :person as well as the variable to
pull the initial values from. But they add in a URL. You need to do
something similar. Probably something like :url =>
details_path(@contact) -- but I'm kinda guessing. I always have to play
with these things for a bit to get something that works.
The "rake routes" command helps me.
Another way to get a handle on this is to view the page in the browser.
On the form, the submit or action is the URL you will post to. That is
what you are trying to change.