English 中文(简体)
同样的目前——设计多种模型的用户
原标题:The same current_user on multiple models in devise

Problem

我有用户模式,志愿人员模式继承用户模式:

class User < ActiveRecord::Base
end

class Volunteer < User
end

在数据库的同一表格上,这两条都得到了节省,但控制器/路线不同。

路线是:

devise_for :users ....
devise_for :volunteers ....

而这种工程是细致的,但我使用的授权制度取决于<代码>的现行_user求助器。 志愿人员也未能做到这一点,因为为志愿人员模式设计了<条码>目前_volunteer。

What i have tried is to set devise_for :volunteers, :singular => "user", and this creates a current_user that refers to users and volunteers, but the problem now is that the routes for volunteers are messed up.

Question

因此,我的问题是,有没有办法制定<编码>目前——用户。 求助者是指用户以外的另一种模式?

最佳回答

我认为,这样一来,就可以让用户在同一届会议上作为用户和沃尔特人签名。

一旦你有办法处理,你就没有。

# in application_controller.rb
alias_method :devise_current_user, :current_user
def current_user
  devise_current_user || current_volunteer
end

页: 1

问题回答

我面临同样的问题,并解决了这一问题。 但是,这一解决办法对于甘蔗是具体的。

申请——主计长。

def actual_user
  @actual_user ||= current_user.present? ? current_user : current_volunteer
end

def current_ability
  @current_ability ||= Ability.new(actual_user)
end

我已经接受了维克多耳机的回答,看来是最清洁的方法。

然而,我以不同的方式解决了我的问题。

I ve ended up hardcoding the sign in process for other classes as :user. This gives me access to the current_user method even for the volunteer class.

class Volunteers::SessionsController < Users::SessionsController

  def create
    resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
    if resource
      flash[:notice] = "You are logged in"
      sign_in(:user, resource)
      super
    else
      super
    end
  end

end

也许稍微晚,但你可以在你的道路上尝试如下。

devise_for :users , :skip => :registrations
as :user do
    match "volunteers/edit(.:format)", :to => "devise/registrations#edit"
end
devise_for :volunteers , :skip => :sessions

上述守则假定,所有用户及其子类别(假定你为达到用户模式等级而实施科学、技术和革新)能够签署、签署、但只有志愿人员才能登记。 由于志愿人员是用户,使用者应当能够照样办理登记手续。 你们可以把更多的路线作为:用户群。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签