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