ruby-on-rails – 允许用户在Devise中添加新用户,并保持登录状态
我正在使用
Ruby On Rails与Devise,Rails 4.1.0.rc1,Ruby 2.1.0p0,devise-3.2.4
我遵循Rails Cast Episode#209的教程,以便设计和工作.我可以登录并注销新用户. 我扩展了我的用户模型,包括不断变化的信息,如birthdate,名字和姓氏. 在这篇博客文章之后,我添加了一个用户控制器&视图,以添加其他功能,如所有我没有看到设备的用户的列表. 我也在Stack overflow中回顾了这些问题: 今年3月1日以来,我已经是轨道上的ruby了,而且已经把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 这是我的应用程序控制器: 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,:birthdate,:current_password)} devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:first_name,:current_password)} end end 这是我的用户new.html.erb文件:(仅管理员只是提醒我,现在没有管理功能) <header id="content-header"> <h1>Create a New User (Admins Only)</h1> </header> <%= render 'form' %> 这是_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 %> 解决方法
我认为问题实际上是与你的路线.我想象你已经添加了资源:用户到你的路由文件,为你的UserController设置路由.但是,我猜你也有devise_for:用户…设置设计.这意味着你会有设计,你的UserController为相同的路线而战.具体来说,您期望POST / /用户创建一个新用户,但它实际上是路由设计的注册控制器并注册一个新用户并进行签名.
您可以通过运行rake路线看到这一点,并看到设计和您的UsersController都有例如POST /users(.:format)的映射. resources :users_admin,:controller => 'users' 这将映射/ users_admin到您的UsersController(但是注意,路径助手也将从例如users_path更改为users_admin_path). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |