Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 设计与帐户和用户的身份验证_Ruby_Ruby On Rails 3_Authentication_Devise - Fatal编程技术网

Ruby 设计与帐户和用户的身份验证

Ruby 设计与帐户和用户的身份验证,ruby,ruby-on-rails-3,authentication,devise,Ruby,Ruby On Rails 3,Authentication,Devise,我正在努力实现一个拥有许多用户的顶级帐户 我正在研究这个问题的解决方案: 工作原理: 用户提交注册表(如下)后,将根据需要创建用户和帐户 什么不起作用: 在重定向到帐户路径之前,未对用户进行身份验证。为了进行身份验证,用户必须单击login并输入他们刚刚注册的凭据。相反,我需要在重定向之前对用户进行身份验证 我已经为此工作了几个小时,尝试了一些方法,但似乎没有任何效果 在成功创建用户/帐户后,是否有人可以帮助我验证用户身份的代码。谢谢 型号 class Account < ActiveRe

我正在努力实现一个拥有许多用户的顶级帐户

我正在研究这个问题的解决方案:

工作原理:

用户提交注册表(如下)后,将根据需要创建用户和帐户

什么不起作用:

在重定向到帐户路径之前,未对用户进行身份验证。为了进行身份验证,用户必须单击login并输入他们刚刚注册的凭据。相反,我需要在重定向之前对用户进行身份验证

我已经为此工作了几个小时,尝试了一些方法,但似乎没有任何效果

在成功创建用户/帐户后,是否有人可以帮助我验证用户身份的代码。谢谢

型号

class Account < ActiveRecord::Base
  has_many :users, :inverse_of => :account, :dependent => :destroy
  accepts_nested_attributes_for :users
  attr_accessible :name, :users_attributes
end

class User < ActiveRecord::Base
  belongs_to :account, :inverse_of => :users
  validates :account, :presence => true
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :lockable, :timeoutable
  attr_accessible :email, :password, :password_confirmation, :remember_me
end
resources :accounts, :only => [:index, :new, :create, :destroy]
controllers/accounts_controller.rb
class AccountsController < ApplicationController

  def new
    @account = Account.new
    @account.users.build # build a blank user or the child form won't display
  end

  def create
    @account = Account.new(params[:account])
    if @account.save
      flash[:success] = "Account created"
      redirect_to accounts_path
    else
      render 'new'
    end
  end

end
控制器

class Account < ActiveRecord::Base
  has_many :users, :inverse_of => :account, :dependent => :destroy
  accepts_nested_attributes_for :users
  attr_accessible :name, :users_attributes
end

class User < ActiveRecord::Base
  belongs_to :account, :inverse_of => :users
  validates :account, :presence => true
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :lockable, :timeoutable
  attr_accessible :email, :password, :password_confirmation, :remember_me
end
resources :accounts, :only => [:index, :new, :create, :destroy]
controllers/accounts_controller.rb
class AccountsController < ApplicationController

  def new
    @account = Account.new
    @account.users.build # build a blank user or the child form won't display
  end

  def create
    @account = Account.new(params[:account])
    if @account.save
      flash[:success] = "Account created"
      redirect_to accounts_path
    else
      render 'new'
    end
  end

end
class AccountsController
视图/accounts/new.html.erb视图

<h2>Create Account</h2>

<%= form_for(@account) do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>

  <%= f.fields_for :users do |user_form| %>
    <div class="field"><%= user_form.label :email %><br />
    <%= user_form.email_field :email %></div>
    <div class="field"><%= user_form.label :password %><br />
    <%= user_form.password_field :password %></div>
    <div class="field"><%= user_form.label :password_confirmation %><br />
    <%= user_form.password_field :password_confirmation %></div>
  <% end %>

  <div class="actions">
    <%= f.submit "Create account" %>
  </div>
<% end %>
创建帐户
f、 对象%>





您需要手动登录用户。在accounts controller中,您需要一个
登录(用户)
,其中
用户
是您要登录的实际
用户
模型记录

问题是,您有一个一个帐户对多个用户的关系。因此,您需要以某种方式访问单个用户,例如:

  def create
    @account = Account.new(params[:account])
    if @account.save
      sign_in(@account.users.first)
      flash[:success] = "Account created"
      redirect_to accounts_path
    else
      render 'new'
    end
  end
您需要添加

  before_filter :authenticate_user!, :except => [:new,:create]

给账户管理员,以便为其余操作提供身份验证

非常感谢。我可能在那里粘贴了24个method/param组合,但似乎找不到合适的用户。这很好用。