Nested resources routing to same action

23 views
Skip to first unread message

ashwin...@telenet.be

unread,
Aug 6, 2013, 7:59:53 AM8/6/13
to rubyonra...@googlegroups.com
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?

Colin Law

unread,
Aug 7, 2013, 9:23:22 AM8/7/13
to rubyonra...@googlegroups.com
On 6 August 2013 12:59, <ashwin...@telenet.be> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> 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.
>
>

Dave Aronson

unread,
Aug 7, 2013, 1:02:47 PM8/7/13
to rubyonra...@googlegroups.com
On Tue, Aug 6, 2013 at 7:59 AM, <ashwin...@telenet.be> 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/.
Reply all
Reply to author
Forward
0 new messages