Question

A User has_many accounts, and an Account belongs_to a Role. A Role can be 'student', 'admin', 'instructor'.

To view instructors, I could do this:

  /accounts?filter_by=instructors

  # controller
  role = params[:role]
  @accounts = Account.joins{role}.where{ role.name.eq(role) }

What I would like do is this:

 /instructors

And, create a route for each type of account. Im struggling a bit figuring out the best way to create these routes.

   resources :accounts do
     collection do
       get "instructors", to: "accounts#index", as: "instructors", default: {filter_by: Role.find_by_name('instructor').id}
    end
  end

How would I do this in rails 4 routes?

Was it helpful?

Solution

I'd guess this:

resources :accounts, path: 'instructors', as: 'instructors', filter_by: 'instructors'

or

get "/:filter_by", 
  constraints: { filter_by: /instructors|another_type/ }, 
  to: "accounts#index", 
  as: "accounts"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top