我使用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
我还在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
我从今年3月1日起开始在轨道上使用ruby,并采取了Mike& amp; 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, :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="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 %>
答案 0 :(得分: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'
会将/users_admin
映射到您的UsersController(但请注意路径助手也会从users_path
更改为users_admin_path
)。
答案 1 :(得分:7)
以下是我最终解决此问题的方法
在routes.rb
中 devise_for :users
resources :users_admin, :controller => 'users'
在users_controller.rb中没有变化
在form.html.erb中添加新的&amp;更新用户。请注意指定的URL。这在某种程度上有文献记载,但只提到它用于单一资源 http://guides.rubyonrails.org/routing.html
然而,我不得不将编辑&amp;新的路径,因为网址在create&amp;同时更新,所以我只有2个表格而不是渲染部分。不确定解决方法是什么。
这是我的新用户表单:
<%= form_for @user, url: users_admin_index_path(@user) do |f| %>
这个是我的编辑用户表单:
<%= form_for @user, url: users_admin_path(@user) do |f| %>
在表单底部我有这段代码:
<% if @user.new_record? %>
<%= f.submit "Create Account" %>
<% else %>
<%= f.submit "Update Account" %>
<% end %>
<%= link_to "Cancel", users_admin_index_path, class: 'button' %>
</p>
然后在show.html.erb表单中,我必须指定来自&#39;佣金路线的网址&#39;
<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>
我从rake路线获得路径名称。
我希望这有助于其他人,并感谢大家的帮助。我还没有足够的声誉来支持任何答案。
答案 2 :(得分:0)
由于我理解了您的问题(文本部分,而不是阅读代码),您希望用户添加其他用户,并且该帐户应该可以使用了。要使用设计进行注册,您将使用用户email, password and password_confirmation field as mandatory
。
使用电子邮件注册用户时,请为帐户提供虚拟密码,以便保存用户条目。使用密码重置链接向用户发送邮件以确认其帐户并更改密码。
答案 3 :(得分:0)
GhostRider在工作流程中是正确的。您也可以将逻辑移动到AdminsController或类似的。该主题将解决以该用户身份登录的具体问题:Devise: How to create a new user being already logged in?
但让管理员设置临时密码并通过电子邮件通知新用户让他们完成创建帐户是一个更好的工作流程。