简体   繁体   中英

What is the best way to separate user settings into multiple pages in Ruby on Rails?

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.

In UsersController itself add methods security, avatar and add routes according to that like,

get "/settings/security", to: "user#security"

如果需要的话, Wicked是可以帮助您创建和处理步骤wizard formgem

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:

  • settings/users/:id/security
  • settings/users/:id/avatar

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:

  • settings/users/security
  • settings/users/avatar

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 (eg A users name and email) but I also wanted a detailed view that showed more data (eg 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM