0

I currently have my URL set up where it will be like this:

localhost:3000/user/3

using this in my routes file:

resources :users, path: '/user' do

Technically I can change /user to anything if I want to change it, for example: /profile will then turn into this:

localhost:3000/profile/3

But what if I have certain options for users at signup to choose from, if they're using my application as a:

Designer, Developer, Assistant

So when user signs up, and chooses from among those three, it'll save into my database under user_as column, and it'll be spelled out in the cells.


So how does do I change /user based on the user_as? I'd like to have either /designer, /developer, or /assistant

hellomello
  • 8,219
  • 39
  • 151
  • 297
  • Nope, you want different controllers with different routes for different models – OneChillDude Feb 05 '14 at 18:00
  • Those sound like user roles to me. I'd look into the [CanCan gem](https://github.com/ryanb/cancan). – eabraham Feb 05 '14 at 18:43
  • Or check out the_role gem:https://github.com/the-teacher/the_role . If you just want the DISPLAY of the URL to change, you could set up routes for the different roles that all point to the same controller. Not sure why you would want that though. – Beartech Feb 05 '14 at 19:02

1 Answers1

0

Check out the section on "Dynamic Segments" in the routing guide: http://guides.rubyonrails.org/routing.html#dynamic-segments

Effectively, you can add variables to your path. For example:

resources :users, path: ':user_type'

When a browser requests /foo/1 it will route to your users controller with user_type "foo". You'll probably want to limit the possible values of user_type to your list above ("designer", "developer", etc. You can do that with segement constraints (http://guides.rubyonrails.org/routing.html#segment-constraints)

However, I'm guessing that your users are created through something like devise, and that you don't actually need to edit users through this interface. If that's true, I would suggest creating add a separate controller with only show and index actions for viewing profiles.

For example, create a ProfilesController with a method, then add to routes.rb

get 'profiles/:user_type' => 'profiles#index'
get 'profiles/:user_type/:id' => 'profiles#show'
Nick Urban
  • 3,568
  • 2
  • 22
  • 36