# Rendering and custom routes

**URL:** <https://www.ruby-forum.com/t/rendering-and-custom-routes/138570>\
**Category:** Rails\
**Created:** [May 24, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/rendering-and-custom-routes/138570 "2008-05-24T22:32:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [May 24, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/rendering-and-custom-routes/138570/1 "2008-05-24T22:32:38Z")

</div>

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](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.

- Wayne

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [May 25, 2008, 2:53am UTC](https://www.ruby-forum.com/t/rendering-and-custom-routes/138570/2 "2008-05-25T02:53:11Z")

</div>

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.

HTH  
pedz

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [May 25, 2008, 3:05am UTC](https://www.ruby-forum.com/t/rendering-and-custom-routes/138570/3 "2008-05-25T03:05:37Z")

</div>

According to the log, it’s rendering contacts (e.g.  
[http://localhost:3000/contacts/1](http://localhost:3000/contacts/1))  
, which is the issue because the URL used to GET the edit template is  
[http://localhost:3000/details/1](http://localhost:3000/details/1)  
. Here’s the form:

\<%= error\_messages\_for :contact %\>

 \<%= content\_tag('h1', @contact.full\_name) %\> 
 \<% form\_for @contact do |f| %\> 

First Name: \<%= f.text\_field :first\_name %\>

 \<%# Remaining tags snipped for brevity %\> 

\<%= submit\_tag "Save Contact" %\>

 \<% end %\> 

 \<%= link\_to '+ Add Note', new\_notes\_path(:contact\_id =\> @contact) %\> \<% if @contact.notes.any? %\> \<% @contact.notes.each do |note| %\> 

\<%=h note.body %\>   
Posted \<%=h distance\_of\_time\_in\_words\_to\_now(note.created\_at) %\>

 \<% end %\> \<% end %\> 

“details” isn’t a controller, it’s just a static part of a named route  
(so I can pretty up the URL and have it display [http://mysite/details/](http://mysite/details/)  
1 instead of [http://mysite/contacts/1/edit](http://mysite/contacts/1/edit)):

map.details “details/:id”, :controller =\> “contacts”, :action =\> “edit”

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 🙂

- Wayne

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [May 25, 2008, 2:55pm UTC](https://www.ruby-forum.com/t/rendering-and-custom-routes/138570/4 "2008-05-25T14:55:44Z")

</div>

Wayne M. wrote:

> ```
> <% form_for @contact do |f| %>
> 
> ```

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:

\<% form\_for :person, @person, :url =\> { :action =\> “update” } do |f| %\>

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.

Good luck.  
pedz
