# Render and routes

**URL:** <https://www.ruby-forum.com/t/render-and-routes/85392>\
**Category:** Rails\
**Created:** [February 13, 2007, 6:28am UTC](https://www.ruby-forum.com/t/render-and-routes/85392 "2007-02-13T06:28:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [February 13, 2007, 6:28am UTC](https://www.ruby-forum.com/t/render-and-routes/85392/1 "2007-02-13T06:28:13Z")

</div>

I have a common situation: a create operation fails and so I want to  
render the new view again for correction. Wouldn’t be too hard to  
render the right thing, but I need to tack an extra parameter onto the  
url. I can’t quite figure how to do this with named routes.

Something like:

render hash\_for\_new\_user\_url(), :username =\> params[:username]

TIA,  
–Dean

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [February 16, 2007, 5:02am UTC](https://www.ruby-forum.com/t/render-and-routes/85392/2 "2007-02-16T05:02:47Z")

</div>

On Feb 13, 12:02 am, “Sheldon H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How about this?
> 
> render hash\_for\_new\_user\_url().merge({:username =\>  
> params[:username]})
> 
> Ciao,  
> Sheldon.

Thanks Sheldon, that made the right hash, but it must not be what I’m  
looking for.

Controller:

render  
hash\_for\_edit\_fermentable\_ingredients\_url().merge(:fermentable\_name =\>  
params[:fermentable\_name] )

Renders without the extra parameter.

Rendering layoutfalseactioneditfermentable\_nameGerman  
Munichonly\_pathfalseuse\_routeedit\_fermentable\_ingredientcontrollerfermentable\_ingredients  
within layouts/application  
Rendering fermentable\_ingredients/edit

Looks like it’s using the extra stuff for the layout instead of  
tacking it on to the request.

Thanks,  
–Dean

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [February 13, 2007, 9:03am UTC](https://www.ruby-forum.com/t/render-and-routes/85392/3 "2007-02-13T09:03:25Z")

</div>

On Feb 13, 7:27 am, “Dean” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Something like:
> 
> render hash\_for\_new\_user\_url(), :username =\> params[:username]

How about this?

render hash\_for\_new\_user\_url().merge({:username =\>  
params[:username]})

Ciao,  
Sheldon.

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [February 16, 2007, 5:45am UTC](https://www.ruby-forum.com/t/render-and-routes/85392/4 "2007-02-16T05:45:49Z")

</div>

You don’t need to pass extra parameters when doing a render. You are  
not calling a url, you are overriding the default rhtml file by  
explicitly calling another one. If you want to pass something into  
that view file then just stick it into an @ variable.

When calling redirect\_to you are constructing a url that will be  
passed to another controller action. In that case you may want to  
tack on extra parameters.

Aaron

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [February 16, 2007, 6:02am UTC](https://www.ruby-forum.com/t/render-and-routes/85392/5 "2007-02-16T06:02:50Z")

</div>

Aaron said the following on 02/15/2007 11:44 PM:

> When calling redirect\_to you are constructing a url that will be  
> passed to another controller action.

Does that mean a redirect\_to() doesn’t actually go out to the client  
browser  
and back again? Ate you saying that its passed along internally and  
the  
other controller sees it “as if” it came from the browser?

In that case does redirect\_to() interact with the routes.rb ?

–  
The intuitive mind is a sacred gift and the rational mind is a faithful  
servant. We have created a society that honors the servant and has  
forgotten the gift.  
–Albert Einstein

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [February 16, 2007, 6:30am UTC](https://www.ruby-forum.com/t/render-and-routes/85392/6 "2007-02-16T06:30:52Z")

</div>

On Feb 15, 8:44 pm, “Aaron” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You don’t need to pass extra parameters when doing a render. You are  
> not calling a url, you are overriding the default rhtml file by  
> explicitly calling another one. If you want to pass something into  
> that view file then just stick it into an @ variable.
> 
> When calling redirect\_to you are constructing a url that will be  
> passed to another controller action. In that case you may want to  
> tack on extra parameters.
> 
> Aaron

Ah, of course. I have it straightened out now. Thanks.

–Dean

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [February 16, 2007, 7:04am UTC](https://www.ruby-forum.com/t/render-and-routes/85392/7 "2007-02-16T07:04:53Z")

</div>

> Does that mean a redirect\_to() doesn’t actually go out to the client browser  
> and back again?

redirect\_to() DOES go back to the browser and redirect it to a new  
page. However, render() does not.

> In that case does redirect\_to() interact with the routes.rb ?

Yes, redirect\_to() is routed just like any other request.

Aaron

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [February 16, 2007, 2:08pm UTC](https://www.ruby-forum.com/t/render-and-routes/85392/8 "2007-02-16T14:08:41Z")

</div>

Aaron said the following on 02/16/2007 01:03 AM:

> > Does that mean a redirect\_to() doesn’t actually go out to the client browser  
> > and back again?
> 
> redirect\_to() DOES go back to the browser and redirect it to a new  
> page. However, render() does not.
> 
> > In that case does redirect\_to() interact with the routes.rb ?
> 
> Yes, redirect\_to() is routed just like any other request.

Ah.  
That was what was making a mess of my error handling.

I had, as I mentioned in an earlier post, tried an “Oops” handler so  
that it  
could handle a vast number of partials & views for the various error  
conditions and have a “method\_missing” for the ones I hadn’t got around  
to  
doing a specific view for.

But without some line in the routes.rb is won’t work.

OBTW: I’m using routes.rb as a security tool - sort-of - by limiting the  
legal routes and format of the parameters. I’m strenuously trying to  
avoid  
‘leaking’ information about internals - names of controls, actions - so  
that  
hackers cant try, for example, stepping through ID numbers.

So am I better with  
if cando = false  
redirect\_to( :controller =\> Oops,  
:action =\> ‘accessdenied’,  
:type =\> ‘alarm’,  
:message =\> ‘no\_permission\_to\_view’,  
:return\_to =\> saved\_url,  
)  
else  
…  
end

or with

if cando false  
render(:template =\> “oops/accessdenied”  
:type =\> ‘alarm’,  
:message =\> ‘no\_permission\_to\_view’,  
:return\_to =\> saved\_url,  
)  
…

Hmm. Perhaps I need to find another way to do the hadnling of the  
‘saved\_url’.

Advice?

–  
Any sufficiently advanced technology is indistinguishable from magic.  
– Arthur C. Clarke
