I am trying to add a custom restful action. I have try the following tutorial but stil get an error and not sure why http://guides.rubyonrails.org/routing.html, http://railscasts.com/episodes/35-custom-rest-actions Here what i am trying to do in my model customer i have def the following def friend @customer = Customer.find(params[:id]) end Here my route resources :customers do collection do get ''friend'' end end Here the link i try so far http://localhost:3000/customers/friend http://localhost:3000/customers/1/friend I also try member but same error occurs. There error is follow ActiveRecord::RecordNotFound in CustomersController#friend Couldn''t find Customer without an ID Thanks in advance -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 13 August 2012 19:11, Jean-Sébastien D. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I am trying to add a custom restful action. I have try the following > tutorial but stil get an error and not sure why > http://guides.rubyonrails.org/routing.html, > http://railscasts.com/episodes/35-custom-rest-actions > > Here what i am trying to do in my model customer > > i have def the following > > def friend > @customer = Customer.find(params[:id]) > end > Here my route > > resources :customers do > collection do > get ''friend'' > end > end > Here the link i try so far > > http://localhost:3000/customers/friend > http://localhost:3000/customers/1/friend > I also try member but same error occurs. There error is follow > > ActiveRecord::RecordNotFound in CustomersController#friend > > Couldn''t find Customer without an IDThat probably means that params[:id] is nil for some reason. First check in development.log where it will show you the parameters passed in when you open the url. Then you will know whether the id is not being passed in or whether you are loosing it somewhere along the way. Next have a look at the Rails Guide on debugging to see techniques that you can use to debug the code. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
I think you want "member" instead of "collection" in your route file. Collection is used when you don''t have an id; member when you do. In your controller you are using the id, so I''m assuming that this is the URL you want: http://localhost:3000/customers/1/friend On Mon, Aug 13, 2012 at 3:42 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 13 August 2012 19:11, Jean-Sébastien D. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> I am trying to add a custom restful action. I have try the following >> tutorial but stil get an error and not sure why >> http://guides.rubyonrails.org/routing.html, >> http://railscasts.com/episodes/35-custom-rest-actions >> >> Here what i am trying to do in my model customer >> >> i have def the following >> >> def friend >> @customer = Customer.find(params[:id]) >> end >> Here my route >> >> resources :customers do >> collection do >> get ''friend'' >> end >> end >> Here the link i try so far >> >> http://localhost:3000/customers/friend >> http://localhost:3000/customers/1/friend >> I also try member but same error occurs. There error is follow >> >> ActiveRecord::RecordNotFound in CustomersController#friend >> >> Couldn''t find Customer without an ID > > That probably means that params[:id] is nil for some reason. First > check in development.log where it will show you the parameters passed > in when you open the url. Then you will know whether the id is not > being passed in or whether you are loosing it somewhere along the way. > Next have a look at the Rails Guide on debugging to see techniques > that you can use to debug the code. > > Colin > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Paul wrote in post #1072262:> I think you want "member" instead of "collection" in your route file. > Collection is used when you don''t have an id; member when you do. In > your controller you are using the id, so I''m assuming that this is the > URL you want: http://localhost:3000/customers/1/friendYeah thank to both of you, member was best and works now :D -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.