Reputation: 12199
I want to fix one question about routing in Rails. There is 'User' model with roles : user.restaurant_owner? and user.shop_owner?. Users are admins only. Also there is a super user (it's me). I want that when user inputs 'http://somesite.com/his_name'
his request is processed the controller 'restaurants' or 'shops' according to his role and name. For example, he inputs 'http://somesite.com/stanly' and he gets view from 'http://somesite.com/restaurants/5', because 'stanly' is owner of restaurant with '5' id; if he inputs 'http://somesite.com/marylin' and he gets view from 'http://somesite.com/shops/101', because 'marylin' is owner of shop with '101' id.
I want to do it, because I want to show user-friendly URL, but my system is very large and has 10 categories (restaurants, cafes, shops, etc ...). Each category has different layout and actions for admin panel.
Please, give me some advice. Thanks.
UPDATES:
Owner can see ONLY his place. There are models restaurants, shops, etc with field 'owner_id'.
Upvotes: 1
Views: 58
Reputation: 56
First, since owner can only see his place, thus only logged in user can visit http://somesite.com/his_name
.
Secondly, you need to have this in your routes.rb
get "/:username" => "users#user_place"
Then, I just assume you have all the classes used below. In your users_controller.rb
, here is the code:
def user_place
# suppose you are using Devise, you should be able to use `current_user`
# authenticate user before this method
unless current_user
redirect_to new_user_session_path
end
restaurant = Restaurant.find_by_user_id current_user.id
if restaurant.present?
redirect_to restaurant
end
# similar code for your other categories
end
Upvotes: 1