Reputation: 63
I'm developing an app using Ruby on Rails and Devise gem which has by default an edit page, but there's some settings that I'd like to separate into another pages, such as "/settings/security", "/settings/avatar" and so on. What is the best way I can do that? Should I create another controller for each page or just more methods? Thank you.
Upvotes: 1
Views: 284
Reputation: 8247
I'd suggest:
If the new view is an alternative view of an existing view, then add a custom view. For example, if I wanted the show view to only display a subset of the controller's resource data (e.g. A users name and email) but I also wanted a detailed view that showed more data (e.g. name, email, address, telephone numbers), then I'd use a custom "details" view on the controller.
If the new view displays information relating to a resource that is attached to the controller's resource, then a new controller should be used.
With your example, if the security view shows the settings that are only relating to security - that's a modified edit view, and I'd use a custom action on the settings controller.
An avatar is likely to be a resource that is attached to the settings resource - so that should have its own controller.
Lastly, if you are looking for the settings to be entered in a number of steps, I'd suggest you look at some of the multi-step techniques.
Upvotes: 0
Reputation: 8247
To extend @Navin's answer, in this situation (where you are adding custom [non-crud] actions to a controller) I'd recommend setting up your routes like this:
namespace :settings do
resources :user do
member do
get :security
get :avatar
end
end
end
That will give you the paths:
This works well if you want to be able to modify other users from these pages.
If you only want to be able to edit the logged in user's attributes, you'll probably want to gather the id via Devise's current_user
method and therefore you won't want the user id in the url. In that case, replace member
with collection
namespace :settings do
resources :user do
collection do
get :security
get :avatar
end
end
end
That will give you the paths:
Upvotes: 0
Reputation: 676
Wicked is the gem
that would help you create and handle step wizard form
if that is what you want.
Upvotes: 0
Reputation: 646
In UsersController itself add methods security, avatar and add routes according to that like,
get "/settings/security", to: "user#security"
Upvotes: 3