允许用户在Devise中添加新用户,并保持自己登录

Sha*_*zam 10 ruby-on-rails devise ruby-on-rails-4

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

我按照Rails Cast Episode#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&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])
        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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

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

<%= render 'form' %>
Run Code Online (Sandbox Code Playgroud)

这是_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 %>
Run Code Online (Sandbox Code Playgroud)

Tim*_*Tim 19

我认为问题实际上是你的路线.我想你已经添加resources :users到你的路线文件中为你的路线设置路线UsersController.但是,我猜你也必须devise_for :users …设置设计.这意味着你将为UsersController同样的路线设计和战斗.具体来说,您希望POST/users可以创建一个新用户,但它实际上是路由到设计的RegistrationsController并注册一个新用户并签名.

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

resources :users_admin, :controller => 'users'
Run Code Online (Sandbox Code Playgroud)

这将映射/users_admin到你的UsersController(但要注意路径佣工也将从例如,改变users_pathusers_admin_path).


Sha*_*zam 7

这是我最终解决这个问题的方法

在routes.rb中

  devise_for :users  
  resources :users_admin, :controller => 'users'
Run Code Online (Sandbox Code Playgroud)

在users_controller.rb中没有任何变化

在form.html.erb中添加新用户和更新用户.请注意指定的URL.这在某种程度上是在这里记录的,但只提到它用于单一资源 http://guides.rubyonrails.org/routing.html

但是我不得不将编辑和新路径分开,因为url在创建和更新的同时不起作用,因此我只有2个表单而不是渲染部分.不确定解决方法是什么.

这是我的新用户表单:

<%= form_for @user, url: users_admin_index_path(@user)  do |f| %> 
Run Code Online (Sandbox Code Playgroud)

这个是在我的编辑用户表单中:

<%= form_for @user, url: users_admin_path(@user)  do |f| %>  
Run Code Online (Sandbox Code Playgroud)

在表单的底部我有这个代码:

 <% if @user.new_record? %>
        <%= f.submit "Create Account" %>
      <% else %>
        <%= f.submit "Update Account" %>
      <% end %>
      <%= link_to "Cancel", users_admin_index_path, class: 'button' %>
    </p>
Run Code Online (Sandbox Code Playgroud)

然后在show.html.erb表单中,我必须从'rake routes'指定URLS

<footer>
  <nav>
    <%= link_to "Edit User", edit_users_admin_path, class: 'button' %> |
    <%= link_to "New User", new_users_admin_path, class: 'button' %> | 
    <%= link_to "All Users", users_admin_index_path, class: 'button' %>
  </nav>
</footer>
Run Code Online (Sandbox Code Playgroud)

我从rake路线获得路径名称.

我希望这有助于其他人,并感谢大家的帮助.我还没有足够的声誉赞成任何答案.