ashwinmertes-CNXmb7IdZIWZIoH1IeqzKA@public.gmane.org
2013-Aug-06 11:59 UTC
Nested resources routing to same action
Hi I''m building a job board that uses a User model and a Job model. A user has many jobs, and a job belongs to a user. I need the board to act like this: If a user is logged in, he can see the jobs he created at /users/:user_id/jobs. If a user is not logged in, he can see all the jobs created by all users at /jobs. Currently I have 2 routes who route to the same index action. *config/routes.rb* * * resources :users do resources :jobs, shallow: true end resources :jobs, only: [:index] *rake routes* * * user_jobs GET /users/:user_id/jobs(.:format) jobs#index jobs GET /jobs(.:format) jobs#index Do I need the index action to conditionally check for the params[:user_id] or is there a better way to accomplish this? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3610385b-f8fa-4709-acb9-a433add7e1e9%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
On 6 August 2013 12:59, <ashwinmertes-CNXmb7IdZIWZIoH1IeqzKA@public.gmane.org> wrote:> Hi > > I''m building a job board that uses a User model and a Job model. A user has > many jobs, and a job belongs to a user. > I need the board to act like this: > > If a user is logged in, he can see the jobs he created at > /users/:user_id/jobs. If a user is not logged in, he can see all the jobs > created by all users at /jobs.You have not stated what a logged in user will see at /jobs and what a non-logged in user will see at /users/:user_id/jobs. Remember anyone can enter any url, and could put in an id for a different user. Why not just have one route and decide what to show based on whether the user is logged in or not? Colin> > > Currently I have 2 routes who route to the same index action. > > config/routes.rb > > resources :users do > resources :jobs, shallow: true > end > resources :jobs, only: [:index] > > rake routes > > user_jobs GET /users/:user_id/jobs(.:format) jobs#index > jobs GET /jobs(.:format) jobs#index > > Do I need the index action to conditionally check for the params[:user_id] > or is there a better way to accomplish this? > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/3610385b-f8fa-4709-acb9-a433add7e1e9%40googlegroups.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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsiroyp1-qYtSs2pQqKVJoOZHQzOz-g4coVb8eiysBmBw%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
On Tue, Aug 6, 2013 at 7:59 AM, <ashwinmertes-CNXmb7IdZIWZIoH1IeqzKA@public.gmane.org> wrote:> If a user is logged in, he can see the jobs he created at > /users/:user_id/jobs. If a user is not logged in, he can see > all the jobs created by all users at /jobs.I would recommend un-nesting them. IMHO, nested resources only make much sense when the inner ones don''t make sense outside the context of the outer ones. (Alternate viewpoints welcome!) Since you allow /jobs, that''s clearly not the case. Anyway, then you can make the "my jobs" URL something like "/jobs?posted_by=123" or even "/jobs?posted_by=me" (and interpret "me" in the controller). That could even be one of many possible job filtering params. -Dave -- Dave Aronson, the T. Rex of Codosaurus LLC, secret-cleared freelance software developer taking contracts in or near NoVa or remote. See information at http://www.Codosaur.us/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAHxKQigx8uKon6j%2BqicEij8i5ny9VjosRwt_g4BM1ErxJa000g%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.