0
因此,我使用的是rails 4,但是使用了protected_attributes ..出于某种原因,当我创建用户时,只有某些属性保存到数据库,如电子邮件和密码。 请参见下面的例子:用户数据没有保存在数据库中
SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation [#<User id: 4, email: "[email protected]", encrypted_password: "$2a$10$x/dpzz6ED9xYwb8zn9dcBO2B/ODizHAu7vNtFwdiYzbT...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2013-11-25 22:34:18", last_sign_in_at: "2013-11-25 22:34:18", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2013-11-25 22:34:18", updated_at: "2013-11-25 22:34:18", zip: nil, gender: nil, first_name: nil, last_name: nil, birthday: nil>]>
这里是我的new.html.erb(使用设计当然)
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.input :email %></div>
<div><%= f.input :password %></div>
<div><%= f.input :password_confirmation %></div>
<div><%= f.input :first_name %></div>
<div><%= f.input :last_name %></div>
<div><%= f.input :zip %></div>
<div><%= f.label :birthday %></div>
<%= f.date_select :birthday,
{:start_year => Time.now.year,
:end_year => 1900,
:use_short_month => true,
:order => [:month, :day, :year],
:prompt => {:month => 'Month', :day => 'Day', :year => 'Year'}},
{:class => 'year',
:id => 'user_birthday'} %>
<label class="radio inline">
<div class="pushm"><%= f.radio_button "gender", "M" %>
Male
</div>
</label>
<label class="radio inline">
<div class="pushf">
<%= f.radio_button "gender", "F" %>
Female
</div>
</label>
<div><%= f.submit "Sign up" %></div><br />
<%= render "devise/shared/links" %>
<% end %>
</div>
User.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :zip, :gender, :remember_me, :first_name, :last_name, :birthday
end
你可以看到zip,性别,生日都不节约。
更新与控制器:
class Users::RegistrationsController < Devise::RegistrationsController
# POST /resource
def create
build_resource
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
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
clean_up_passwords resource
respond_with resource
end
end
def after_sign_up_path_for(resource)
welcome_path
end
end
你可以发布你的控制器代码? – kddeisz
当然,添加到原始帖子。 –
当你创建一个用户时,你确定你为这些字段指定了一个值?你没有进行验证,所以它们不是必需的。 – kddeisz