我有一个标准内容的用户注册表单。它工作正常,但现在它完全坏了,错误消息完全没有用。几个星期前我跳进了这个现有的应用程序。即使我在将任何代码放入此项目之前检出提交,表单仍然不起作用。通过'不工作',我的意思是它会抛出错误,好像表格完全是空白的('请输入电子邮件','请输入密码'),当他们肯定不是空白时。这是相关的代码......
MODEL:
include FormsHelper
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation, :remember_me,
:first_name, :last_name, :gender, :time_zone, :state,
:birthday, :oauth_id, :oauth_provider_display_name,
:admin, :vagrant, :favorite_shoe, :race_manager, :auto_submission,
:country
控制器:
def create
build_resource
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
Rails.logger.info(resource.errors.inspect)
flash.now[:error] = I18n.t('activerecord.template.body')
respond_with resource
end
end
查看
<%= f.input :email, :required => true,
:autofocus => true,
:input_html => { :autocomplete => 'off' },
:as => 'email' %>
<% if resource.oauth_id.present? %>
<%#
Check if we're coming here from an OAuth-typ registration. The value here doesn't really matter
because it'l be auto-generated again on create. This is just for form validation purposes.
%>
<%= f.hidden_field :password %>
<%= f.hidden_field :password_confirmation,
:value => f.object.password %>
<% else %>
<%= f.input :password,
:required => true,
:hint => controller.controller_name == 'account' ? "Leave blank if you don't want to update your password." : nil,
:input_html => { :value => f.object.password, :autocomplete => 'off' } %>
<%= f.input :password_confirmation,
:label => "Confirm password",
:required => true,
:input_html => { :value => f.object.password_confirmation, :autocomplete => 'off' } %>
<% end %>
<%= f.input :first_name, :required => true %>
<%= f.input :last_name, :required => true %>
这是日志输出
Started POST "/users" for 127.0.0.1 at 2013-12-31 16:20:43 -0800
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"IR6YzpsgwrfLwP0snZ7l1c6RTjhtv/tOtDtHp/I1Agc=", "user"=>{"oauth_id"=>"", "oauth_provider_display_name"=>"", "email"=>"blakester99999@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "first_name"=>"Hey", "last_name"=>"There", "country"=>"United States", "state"=>"CA", "gender"=>"m", "birthday(2i)"=>"1", "birthday(3i)"=>"1", "birthday(1i)"=>"2003", "time_zone"=>"Eastern Time (US & Canada)", "favorite_shoe"=>"Asics"}, "commit"=>"Complete Registration"}
(0.2ms) BEGIN
(0.3ms) ROLLBACK
#<ActiveModel::Errors:0x007ffdaf5babd0 @base=#<User id: nil, email: "", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, admin: false, first_name: nil, last_name: nil, gender: nil, state: nil, birthday: nil, time_zone: nil, vagrant: nil, favorite_shoe: nil, shipping_address_id: nil, race_manager: false, auto_submission: true, confirmation_token: nil, confirmed_at: nil, confirmation_sent_at: nil, unconfirmed_email: nil, referral_credit: #<BigDecimal:7ffdaf4ff920,'0.0',9(18)>, country: nil>, @messages={:email=>["Enter your email address."], :password=>["Enter a password."]}>
这里令人困惑的是,参数明显地出现在正确的位置。但后来它只是回滚,模型上没有任何内容。我甚至关闭了所有验证,我仍然收到错误,说需要填写电子邮件和密码(当它们显然已经填写时,如params所示)。
非常感谢任何想法!这个让我疯狂。谢谢!
更新:根据用户的建议,我检查了宝石问题。在我做任何事之前我回滚过提交,然后运行bundle install。唯一的变化是来自3.0的“Paperclip”更新 - &gt; 3.5.2。但这并没有解决任何问题。和以前一样的问题。然而,通过验证,它确实产生了这个日志输出......
(0.3ms) BEGIN
User Exists (0.5ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY '' LIMIT 1
(0.7ms) ROLLBACK
请注意,该电子邮件是BINARY''。通常情况下,实际的电子邮件地址应显示在“是”的位置。而且,params肯定会在日志中完全显示为类型。然而,当它进行SQL检查时,字段全部为空。有什么想法吗?
答案 0 :(得分:2)
猜测你正在使用Devise,因为控制器代码几乎与Devise::RegistrationsController
相同。 (current code here)
几乎 - 除了build_resource
传递传入参数的部分。
您可能想要检查应用程序的工作副本(其他开发人员?生产?)正在使用的Devise版本,因为某些旧版本的build_resource
方法不需要参数。