我正在使用自定义控制器来处理Devise操作。设计验证不工作Rails 4
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/[email protected]/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.8ms)
Rendered /Users/jonathantrope/.rvm/gems/[email protected]/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /Users/jonathantrope/.rvm/gems/[email protected]/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.1ms)
Rendered /Users/jonathantrope/.rvm/gems/[email protected]/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (19.2ms)
感谢。
请在注册用户时分享您的注册新视图和服务器日志。 –