允许用户在 Devise 中添加新用户,并以他们自己的身份保持登录状态 [英] Allow a user to add new users in Devise, and remain logged in as themselves

查看:21
本文介绍了允许用户在 Devise 中添加新用户,并以他们自己的身份保持登录状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Ruby On Rails 与 Devise、Rails 4.1.0.rc1、Ruby 2.1.0p0、devise-3.2.4 一起使用

我按照 Rails Cast 第 209 集的教程安装并运行了设备.我可以登录和注销并注册新用户.

我扩展了我的用户模型以包含不变的信息,例如生日、名字和姓氏.

在这篇博文之后,我添加了一个用户控制器 &视图以添加其他功能,例如我在设计中没有看到的所有用户的列表.https://github.com/danweller18/devise/wiki/Allow-Users-to-View-Profile-and-List-All-Users

我还回顾了有关堆栈溢出的这些问题:允许管理员使用 Devise 添加用户如何自定义控制器以在 Devise 中注册?Ruby on Rails:自定义设计注册控制器,请求创建操作

我从今年 3 月 1 日开始接触 ruby​​ on rails,并开始学习 Mike &妮可·克拉克斯 Rails 1 &Rails 2 在线课程.

我想做的是允许用户添加新用户.最终,此功能将是添加客户端的管理员或经理.然后客户端应该能够使用创建的凭据登录.

发生的事情是我可以登录,通过 new_user_path 和用户视图(而不是设计视图)添加一个新的用户",但是当我提交它时,我以新用户的身份登录.以前的用户不是持久的.

我通过用户视图来完成所有这些操作控制器操作,因为我不想实际注册"或登录 &使用这些操作注销,只需在用户表中创建新记录,然后就可以自己登录了.

感谢任何帮助.

这是我的用户控制器:

class UsersController <应用控制器定义索引@users = User.all结尾高清秀@user = User.find_by_id(params[:id])结尾定义新@user = User.new结尾定义编辑@user = User.find(params[:id])结尾定义更新@user = User.find(params[:id])如果@user.update_attributes(user_params)redirect_to user_url,注意:更新的用户."别的渲染:编辑结尾结尾定义创建@user = User.new(user_params)如果@user.saveredirect_to user_url,注意:用户创建成功!"别的渲染:新结尾结尾私人的定义用户参数params.require(:user).permit(:first_name, :last_name, :img_file_name, :email, :password, :password_confirmation, :birthdate)结尾结尾

这是我的应用程序控制器:

class ApplicationController <动作控制器::基础# 通过引发异常来防止 CSRF 攻击.# 对于 API,您可能希望使用 :null_session 代替.保护_从_伪造与::异常before_action :configure_permitted_pa​​rameters, if: :devise_controller?受保护def configure_permitted_pa​​rametersdevise_parameter_sanitizer.for(:account_update) {|u|u.permit(:first_name,:last_name,:birthdate,:img_file_name,:email,:password,:password_confirmation,:current_password)}devise_parameter_sanitizer.for(:sign_up) {|u|u.permit(:first_name,:last_name,:birthdate,:img_file_name,:email,:password,:password_confirmation,:current_password)}结尾结尾

这是我的用户 new.html.erb 文件:(Admins Only 只是对我的提醒,目前没有管理功能)

<h1>创建新用户(仅限管理员)</h1></标题><%= 渲染表单"%>

这里是_form.html.erb

<%= form_for(@user) do |f|%><%= 渲染共享/错误",对象:@user %><字段集><ol><li class="必需"><%= f.label :first_name %><%= f.text_field :first_name, size: 40, autofocus: true %><li class="必需"><%= f.label :last_name %><%= f.text_field :last_name, size: 40%><li class="必需"><%= f.label :email %><%= f.email_field :email, size: 40%><li class="必需"><%= f.label :password %><%= f.password_field :password, size: 40%><li class="必需"><%= f.label :password_confirmation, "确认密码" %><%= f.password_field :password_confirmation, size: 40%><li ><%= f.label :birthdate %><br/><%= f.date_select :birthdate, start_year: 1915 %><br/><li ><%= f.label :img_file_name %><br/><%= f.text_field :img_file_name %></ol><p><% 如果@user.new_record?%><%= f.submit "创建账户" %><%其他%><%= f.submit "更新账户" %><%结束%><%= link_to "Cancel", users_path, class: 'button' %></p></fieldset><%结束%>

解决方案

我认为问题实际上出在您的路线上.我想您已经将 resources :users 添加到您的路由文件来为您的 UsersController 设置路由.但是,我猜您还需要 devise_for :users ... 来设置设计.这意味着您将拥有 devise 和您的 UsersController 争取相同的路线.具体来说,您希望向/users 发送 POST 以创建一个新用户,但它实际上是路由到设计的 RegistrationsController 并注册一个新用户并让他们登录.

您可以通过运行 rake routes 并看到设计和您的 UsersController 都具有例如 POST/users(.:format) 的映射来看到这一点.您需要将两组路由分开.有多种方法可以做到这一点.您可以添加 :path =>'u'routes.rb 中的 devise_for 行,这意味着你的设计路由都是 /u 而不是<代码>/用户.或者,您可以在 /users 上保留设计路由,而是将您的 UsersController 路由更改为:

resources :users_admin, :controller =>'用户'

这会将 /users_admin 映射到您的 UsersController(但请注意,路径助手也会从例如 users_path 更改为 users_admin_path).

I am using Ruby On Rails with Devise, Rails 4.1.0.rc1, Ruby 2.1.0p0, devise-3.2.4

I followed the tutorial from Rails Cast Episode #209 to get devise installed and working. I can login and log out and register new users.

I extended my user model to include non-changing information like birthdate, first name and last name.

Following this blog post I added a user controller & views in order to add additional functionality such as a listing of all users that I did not see with devise. https://github.com/danweller18/devise/wiki/Allow-Users-to-View-Profile-and-List-All-Users

I have also reviewed these questions on Stack overflow: Allowing admins to add users with Devise How do I customize the controller for registration in Devise? Ruby on Rails: Custom Devise Registration Controller, Asking For Create Action

I am new to ruby on rails since March 1st of this year, and have taken Mike & Nicole Clarks Rails 1 & Rails 2 online courses.

What I am trying to do is allow a user to add new users. Ultimately this function would be an admin or manager adding clients. The client should then be able to log in with the credentials created.

What is occurring is that I can be logged in, add a new "user" through the new_user_path and user view (as opposed to the devise view) but when I submit it, I am then logged in as the new user. The previous users is not persistent.

I am doing all of this via the users view & controller actions because I don't want to actually "register" or login & logout with these actions, just create new records in the user table that would then be able to log in on their own.

Any help is appreciated.

here is my user controller:

class UsersController < ApplicationController

    def index
        @users = User.all
    end

    def show
      @user = User.find_by_id(params[:id])
    end

    def new
        @user = User.new
    end

    def edit
        @user = User.find(params[:id])
    end

    def update
        @user = User.find(params[:id])
        if @user.update_attributes(user_params)
            redirect_to user_url, notice: "Updated User."
        else
            render :edit
        end
    end

    def create
        @user = User.new(user_params)
        if @user.save
            redirect_to user_url, notice: "User succesfully created!" 
        else
            render :new
        end
    end






private

def user_params
  params.require(:user).permit(:first_name, :last_name, :img_file_name, :email, :password, :password_confirmation, :birthdate)
end
end

Here is my application controller:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

before_action :configure_permitted_parameters, if: :devise_controller?


protected

    def configure_permitted_parameters
        devise_parameter_sanitizer.for(:account_update) {|u| 
            u.permit(:first_name, :last_name, :birthdate, :img_file_name, :email, :password, :password_confirmation, :current_password)}
        devise_parameter_sanitizer.for(:sign_up) {|u| 
            u.permit(:first_name, :last_name, :birthdate, :img_file_name, :email, :password, :password_confirmation, :current_password)}
    end




end

Here is my user new.html.erb file: (Admins Only is just a reminder for me right now, there is no admin function at them moment)

<header id="content-header">
  <h1>Create a New User (Admins Only)</h1>
</header>

<%= render 'form' %>

Here is the _form.html.erb

<%= form_for(@user) do |f| %>
  <%= render "shared/errors", object: @user %>
  <fieldset>
    <ol>

      <li class="required">
        <%= f.label :first_name %>
        <%= f.text_field :first_name, size: 40, autofocus: true %>
      </li>
      <li class="required">
        <%= f.label :last_name %>
        <%= f.text_field :last_name, size: 40 %>
      </li>
      <li class="required">
        <%= f.label :email %>
        <%= f.email_field :email, size: 40 %>
      </li>
      <li class="required">
        <%= f.label :password %>
        <%= f.password_field :password, size: 40 %>
      </li>
      <li class="required">
        <%= f.label :password_confirmation, "Confirm Password" %>
        <%= f.password_field :password_confirmation, size: 40 %>
      </li>
      <li >
        <%= f.label :birthdate %><br/>
        <%= f.date_select :birthdate, start_year: 1915 %><br/>
      </li>

      <li >
          <%= f.label :img_file_name %><br/>
          <%= f.text_field :img_file_name %>
      </li>

    </ol>
    <p>
      <% if @user.new_record? %>
        <%= f.submit "Create Account" %>
      <% else %>
        <%= f.submit "Update Account" %>
      <% end %>
      <%= link_to "Cancel", users_path, class: 'button' %>
    </p>
  </fieldset>
<% end %>

解决方案

I think the problem is actually with your routes. I imagine that you've added resources :users to your routes file to set up routes for your UsersController. However, I'm guessing that you've also got devise_for :users … to set up devise. This means that you'll have devise and your UsersController fighting for the same routes. Specifically, you're expecting a POST to /users to create a new user, but it's actually routing to devise's RegistrationsController and registering a new user and signing them in.

You can see this by running rake routes and seeing that both devise and your UsersController have, for example, mappings for POST /users(.:format). You need to separate out the two sets of routes. There are various ways you can do this. You could add :path => 'u' to your devise_for line in routes.rb, which will then mean your devise routes are all /u instead of /users. Alternatively, you could keep devise routes on /users and instead change your UsersController routes to something like:

resources :users_admin, :controller => 'users'

which will map /users_admin to your UsersController (but note the path helpers will also change from e.g. users_path to users_admin_path).

这篇关于允许用户在 Devise 中添加新用户,并以他们自己的身份保持登录状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆