使用用户控制器的设计视图

时间:2013-11-22 09:51:51

标签: ruby-on-rails devise ruby-on-rails-3.2

我正在使用设计用户管理。我已经定制了设计视图,我对它们很满意。但是,当用户想要编辑她的个人资料时,她会转到/users/edit,这很好。但是当管理员想要查看该用户的个人资料时(其他用户无法看到彼此的个人资料),她会转到/users/2/edit(假设2是该用户的ID)。我想使用设计中的相同视图(位于/app/ views/devise/registrations/edit.html.erb),而不是/app/views/users/edit.html中的单独视图。

我可以将设计视图中的内容分解为部分,但我不知道如何提供resourceresource_name(以及其他)变量。

这必须是重复的,但我缺乏搜索正确答案的词汇。


routes.rb(根据devise faq错误,我认为;不要复制粘贴!):

scope "(:locale)", locale: /en|ro/ do
    devise_for :users
    resources :users, :only =>[:index, :edit]
end # scope "/:locale"

路线如下:

                    home GET    (/:locale)/home(.:format)                static_pages#home {:locale=>/en|ro/}
        new_user_session GET    (/:locale)/users/sign_in(.:format)       devise/sessions#new {:locale=>/en|ro/}
            user_session POST   (/:locale)/users/sign_in(.:format)       devise/sessions#create {:locale=>/en|ro/}
    destroy_user_session DELETE (/:locale)/users/sign_out(.:format)      devise/sessions#destroy {:locale=>/en|ro/}
           user_password POST   (/:locale)/users/password(.:format)      devise/passwords#create {:locale=>/en|ro/}
       new_user_password GET    (/:locale)/users/password/new(.:format)  devise/passwords#new {:locale=>/en|ro/}
      edit_user_password GET    (/:locale)/users/password/edit(.:format) devise/passwords#edit {:locale=>/en|ro/}
                         PUT    (/:locale)/users/password(.:format)      devise/passwords#update {:locale=>/en|ro/}
cancel_user_registration GET    (/:locale)/users/cancel(.:format)        devise/registrations#cancel {:locale=>/en|ro/}
       user_registration POST   (/:locale)/users(.:format)               devise/registrations#create {:locale=>/en|ro/}
   new_user_registration GET    (/:locale)/users/sign_up(.:format)       devise/registrations#new {:locale=>/en|ro/}
  edit_user_registration GET    (/:locale)/users/edit(.:format)          devise/registrations#edit {:locale=>/en|ro/}
                         PUT    (/:locale)/users(.:format)               devise/registrations#update {:locale=>/en|ro/}
                         DELETE (/:locale)/users(.:format)               devise/registrations#destroy {:locale=>/en|ro/}
             user_unlock POST   (/:locale)/users/unlock(.:format)        devise/unlocks#create {:locale=>/en|ro/}
         new_user_unlock GET    (/:locale)/users/unlock/new(.:format)    devise/unlocks#new {:locale=>/en|ro/}
                         GET    (/:locale)/users/unlock(.:format)        devise/unlocks#show {:locale=>/en|ro/}
                   users GET    (/:locale)/users(.:format)               users#index {:locale=>/en|ro/}
               edit_user GET    (/:locale)/users/:id/edit(.:format)      users#edit {:locale=>/en|ro/}
                         GET    /:locale(.:format)                       static_pages#home
                         root   /                                        static_pages#home

用户模型:

class User < ActiveRecord::Base
    devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :lockable
    attr_accessible :id, :name, :email, :password, 
        :password_confirmation, :remember_me
    as_enum :usertype, :regular => 0, :admin => 1, :superadmin => 2
    before_validation :make_superadmin
    def adminkind?
        return (self.usertype_cd == User.admin) || 
            (self.usertype_cd == User::superadmin)
    end
  private
    def make_superadmin
        if User.count == 0
            self.usertype_cd = User.superadmin
        end
    end
end

用户控制器:

class UsersController < ApplicationController
    def index
        respond_to do |format|
            format.html
            format.json {
                render json: UsersDatatable.new(view_context) }
        end
    end
    def edit
    end
end

0 个答案:

没有答案