我正在使用自定义控制器来处理Devise操作。
class RegistrationsController < Devise::RegistrationsController
def new
@joining_group = params[:access_token]
session[:access_token] = params[:access_token]
super
end
def create
super
sign_in resource_name, resource # sign in the user so that the code below works
if params[:organization] # User is an admin and creates group
current_user.admin = true
new_group = Group.create( name: params[:organization], access_token: SecureRandom.hex ) # add super secret token to group so other users can be invited to join
current_user.group = new_group
current_user.save!
else # user is joining vis a vis email invitation with access_token
current_user.group_id = Group.where( access_token: session[:access_token] ).first.id
current_user.save!
end
end
def update
super
end
def group_params
params.require(:group).permit(:name)
end
end
然而,当我尝试创建一个新用户时,当我有一个空输入标记时,我的浏览器会出现以下错误(如果所有输入都已填充,则创建工作正常):
NoMethodError in RegistrationsController#create
undefined method `admin=' for nil:NilClass
Extracted source (around line #12):
10 super
11 if params[:organization] # User is an admin and creates group
12 current_user.admin = true
13 new_group = Group.create( name: params[:organization], access_token: SecureRandom.hex )
14 current_user.group = new_group
15 current_user.save!
我的用户类中已启用validatable模块。我的注册/ new.html.erb视图是这样的:
<div class="logo">
<h1>VitalTracker</h1>
</div>
<div class="registration_box">
<% if @joining_group %>
<h2>Join Group</h2>
<% else %>
<h2>Create Account</h2>
<% end %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div>
<%= f.email_field :email, :autofocus => true, placeholder: "Email", class: "registration_input" %>
</div>
<div>
<%= f.password_field :password, placeholder: "Password", class: "registration_input" %>
</div>
<div>
<%= f.password_field :password_confirmation, placeholder: "Confirm Password", class: "registration_input" %>
</div>
<div>
<%= f.text_field :name, placeholder: "Name", class: "registration_input" %>
</div>
<% unless @joining_group %>
<div>
<%= text_field_tag "organization", nil, placeholder: "Organization", class: "registration_input" %>
</div>
<% end %>
<div>
<%= f.submit "Create Account", class: "registration_btn large_btn" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
下面的服务器日志:
Started POST "/users" for 127.0.0.1 at 2014-02-22 16:05:56 -0500
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"eBTmAyknoi92qdbhiirlUfjjtbdOiD8jzYwp6ZE9dwI=", "user"=>{"email"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "name"=>""}, "organization"=>"", "commit"=>"Create Account"}
(0.2ms) BEGIN
(0.2ms) ROLLBACK
Rendered devise/shared/_links.erb (0.3ms)
Rendered registrations/new.html.erb within layouts/application (3.8ms)
(0.2ms) BEGIN
SQL (0.7ms) INSERT INTO "users" ("created_at", "current_sign_in_at", "current_sign_in_ip", "last_sign_in_at", "last_sign_in_ip", "name", "sign_in_count", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["created_at", Sat, 22 Feb 2014 21:05:56 UTC +00:00], ["current_sign_in_at", Sat, 22 Feb 2014 21:05:56 UTC +00:00], ["current_sign_in_ip", "127.0.0.1"], ["last_sign_in_at", Sat, 22 Feb 2014 21:05:56 UTC +00:00], ["last_sign_in_ip", "127.0.0.1"], ["name", ""], ["sign_in_count", 1], ["updated_at", Sat, 22 Feb 2014 21:05:56 UTC +00:00]]
(0.6ms) COMMIT
(0.2ms) BEGIN
SQL (0.6ms) INSERT INTO "groups" ("access_token", "created_at", "name", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["access_token", "103ff607863b1f766d3cd1b23d1f57dd"], ["created_at", Sat, 22 Feb 2014 21:05:56 UTC +00:00], ["name", ""], ["updated_at", Sat, 22 Feb 2014 21:05:56 UTC +00:00]]
(0.4ms) COMMIT
(0.2ms) BEGIN
(0.2ms) ROLLBACK
Completed 422 Unprocessable Entity in 94ms
ActiveRecord::RecordInvalid (Validation failed: Email can't be blank):
app/controllers/registrations_controller.rb:17:in `create'
Rendered /Users/jonathantrope/.rvm/gems/ruby-2.0.0-p353@rails4/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.8ms)
Rendered /Users/jonathantrope/.rvm/gems/ruby-2.0.0-p353@rails4/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /Users/jonathantrope/.rvm/gems/ruby-2.0.0-p353@rails4/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.1ms)
Rendered /Users/jonathantrope/.rvm/gems/ruby-2.0.0-p353@rails4/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (19.2ms)
感谢。
答案 0 :(得分:0)
如果您要使用current_user
,请将before_filter :authenticate_user!
添加到您的控制器。查看Devise::RegistrationsController
的{{3}}我认为您希望使用resource
代替current_user
。
顺便说一下,根据params[:organization]
创建管理员是一个坏主意。
答案 1 :(得分:0)
current_user
为零,因为没有人登录。
你需要这样的东西:
def create
super
# sign in after registration
sign_in resource_name, resource
# then the rest of your code shuold be ok
if params[:organization] # User is an admin and creates group
current_user.admin = true
# etc
答案 2 :(得分:0)
问题是由于调用current_user.save !,这阻止了无效的创建错误消息在视图中按预期显示。使用current_user.save修复它。