# REST params for new action

**URL:** <https://www.ruby-forum.com/t/rest-params-for-new-action/118436>\
**Category:** Rails\
**Created:** [October 26, 2007, 10:13pm UTC](https://www.ruby-forum.com/t/rest-params-for-new-action/118436 "2007-10-26T22:13:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 26, 2007, 10:13pm UTC](https://www.ruby-forum.com/t/rest-params-for-new-action/118436/1 "2007-10-26T22:13:16Z")

</div>

Hey all,

I need to pass an id to a new REST action which creates an image for a  
user. So I need to pass the user id to the REST action in order to  
create the image for that user, i’m trying this

view:

\<%= link\_to ‘Add Picture’, ‘/user\_images/new’, :user\_id =\> @user.id %\>

action (/user\_images/new):

def new  
@user = User.find(@params[:user\_id])  
end

thats not working, the exception says Couldn’t find User without an ID

so, how do I pass a parameter to the new rest action ? i know the  
other rest actions work something like user\_path(@user) bit that does  
not apply to the new action

anyone?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 26, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/rest-params-for-new-action/118436/2 "2007-10-26T22:45:27Z")

</div>

sounds like you might want to use a nested resource, since a user  
has\_many images… In which case you’d use something like:

link\_to ‘New Pictures’, new\_image\_path(@user)

there’s a very good tutorial application which explains exactly how to  
do this (as well as use polymorphic associations in case you wanted to  
store more than just images with the user). You can find it here:

[http://sample.caboo.se/empty\_rails\_app/trunk/](http://sample.caboo.se/empty_rails_app/trunk/)

Adam

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 27, 2007, 12:55am UTC](https://www.ruby-forum.com/t/rest-params-for-new-action/118436/3 "2007-10-27T00:55:54Z")

</div>

Thank u so much mike, that was exactly what I needed, however your  
link didnt work :S

one more question, I added this to my routes.rb:

map.resources :users do |users|  
users.resources :user\_images  
end

link in the view:

\<%= link\_to ‘Add a picture’, new\_user\_image\_path(@user) %\>

the weird thing comes in the new action in the user\_image controller,  
the only way I can find the user data is using this

def new  
@user = User.find(@params[:user\_id])  
end

It only works when I use @params[:user\_id] dont know why, if i try  
@params[:user] or @params[:id] it will give me a Couldn’t find Place  
without an ID exception.

any idea why it only works with that weird param name ?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 27, 2007, 9:33am UTC](https://www.ruby-forum.com/t/rest-params-for-new-action/118436/4 "2007-10-27T09:33:33Z")

</div>

> I need to pass the user id to the REST action in order to  
> create the image for that user, i’m trying this
> 
> action (/user\_images/new):
> 
> def new  
> @user = User.find(@params[:user\_id])  
> end

Could it be because you’re calling @params instead of params?

–Andrew V.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 29, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/rest-params-for-new-action/118436/5 "2007-10-29T16:20:08Z")

</div>

Mike, thank you very much. Your info has been very helpful.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 27, 2007, 1:47am UTC](https://www.ruby-forum.com/t/rest-params-for-new-action/118436/6 "2007-10-27T01:47:18Z")

</div>

On 10/26/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank u so much mike, that was exactly what I needed, however your  
> link didnt work :S

sorry, I should’ve been more descriptive with the link… The link I  
posted is the rails sample application. You can find the svn  
repository for it here:

svn://caboo.se/plugins/court3nay/empty\_apps/restful\_auth\_rspec

you should be able to issue an svn co to that url and obtain a local  
copy of the sample application.

> one more question, I added this to my routes.rb:
> 
> map.resources :users do |users|  
> users.resources :user\_images  
> end

the convention for using nested resources would normally be to name  
your nested controller simply ‘images’, since the nested declaration  
implicitly shows that images belong to users. Also, the url generated  
by the above would be ‘/users//user\_images/’ which I think is  
less attractive than ‘/users//images/’

> ```
> @user = User.find(@params[:user_id])
> 
> ```
> 
> end
> 
> It only works when I use @params[:user\_id] dont know why, if i try  
> @params[:user] or @params[:id] it will give me a Couldn’t find Place  
> without an ID exception.

this is the expected behaviour for nested resources. When the nested  
controller sees a request such as ‘/users/17/images/new’, the id for  
the user will be passed under params[:user\_id]. If, however, you were  
going to call the show action for an individual image, such as with  
the following route: ‘/users/17/images/20’, then you’d receive the  
value 17 for params[:user\_id] and 20 would be passed as params[:id].

When dealing with nested restful controllers, it’s common to use a  
before\_filter in the nestee which grabs the parent object. For  
example, in your images controller, you’d use the following:

before\_filter :get\_user

private  
def get\_user  
@user = User.find\_by\_id(params[:user\_id])  
end

public  
public restful methods go here

and then in your create method, you’d use something like:

def create

# no need to find the user, the before filter takes care of that

@image = @user.images.build(params[:image])  
…  
@image.save!  
…  
end

Again, a lot of this is covered in the caboo.se sample rails  
application, although they make things more flexible by using a  
polymorphic association, which means instead of using an ‘images’  
nested controller, they use ‘assets’ so a user can have any type of  
file associated with them, it’s not limited to just images.
