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

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

问题描述

我正在使用Ruby On Rails与Devise,Rails 4.1.0.rc1,Ruby 2.1.0p0,devise-3.2.4



我遵循了Rails的教程铸造剧集#209,让设计安装和工作。我可以登录并注销新用户。



我扩展了我的用户模型,包括不断变化的信息,如birthdate,名字和姓氏。



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



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



我是新的ruby在rails上今年3月1日,麦克& Nicole Clarks Rails 1& Rails 2在线课程。



我想要做的是允许用户添加新用户。最终这个功能将是管理员或管理员添加客户端。然后客户端可以使用创建的凭据登录。



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



我正在通过用户视图&控制器动作,因为我不想实际注册或登录&注销这些操作,只需在用户表中创建新的记录,然后才能自己登录。



任何帮助都不胜感激。



这是我的用户控制器:

  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])
如果@user。 update_attributes(user_params)
redirect_to user_url,注意:更新的用户。
else
render:edit
end
end

def create
@user = User.new(user_params)
if @ user.save
redirect_to user_url,注意:用户成功创建!
else
呈现:新
结束
结束






private

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

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

  class ApplicationController< ActionController :: Base 
#通过引发异常来防止CSRF攻击。
#对于API,您可能需要使用:null_session。
protect_from_forgery with::exception

before_action:configure_permitted_pa​​rameters,如果::devise_controller?


protected

def configure_permitted_pa​​rameters
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

这是我的用户new.html.erb文件:(仅管理员只是提醒我,现在没有管理功能)

 < header id =内容报头> 
< h1>创建新用户(仅限管理员)< / h1>
< / header>

<%= render'form'%>

这是_form.html.erb

 <%= form_for(@user)do | f | %GT; 
<%= rendershared / errors,对象:@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,确认密码%>
<%= 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? %GT;
<%= f.submit创建帐户%>
<%else%>
<%= f.submit更新帐户%>
<%end%>
<%= link_to取消,users_path,class:'button'%>
< / p>
< / fieldset>
<%end%>


解决方案

我认为问题实际上是与您的路由。我想像您已经将资源:用户添加到您的路由文件中,为您的 UsersController 设置路由。但是,我猜你也有 devise_for:users ... 设置设计。这意味着你会有一些设计,你的 UsersController 为相同的路线而战。具体来说,您期望POST /用户创建一个新用户,但实际上是路由到设计的注册控制器并注册新用户并签名。



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

  resources:users_admin,:controller => 'users'

它将映射 / 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天全站免登陆