我有两个模型,一个是User,另一个是UserProfile。 UserProfile属于User,User有一个UserProfile。当用户注册时,它是收集所有信息的一种形式,信息分为两种不同的模型。我似乎无法实现这一目标。我在用户模型上使用了accepts_nested_attributes_for:user_profile。仍然只保存用户模型,但不保存UserProfile 我的观点:
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control'%>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control'%>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control'%>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control'%>
<h3> Profile Information</h3>
<%= f.fields_for @user.user_profile do |profile_form| %>
<%= profile_form.label :age %>
<%= profile_form.text_field :age, class: 'form-control' %>
<%= profile_form.label :height %>
<%= profile_form.text_field :height, class: 'form-control' %>
<%= profile_form.label :contact %>
<%= profile_form.text_field :contact, class: 'form-control'%>
<%= profile_form.label :city %>
<%= profile_form.text_field :city, class: 'form-control'%>
<%= profile_form.label :state %>
<%= profile_form.text_field :state, class: 'form-control'%>
<%= profile_form.label :batting_style %>
<%= profile_form.text_field :batting_style, class: 'form-control'%>
<%= profile_form.label :bowling_style %>
<%= profile_form.text_field :bowling_style, class: 'form-control'%>
<%= profile_form.label :favorite_players %>
<%= profile_form.text_field :fav_players, class: 'form-control'%>
<%= profile_form.label :favorite_team %>
<%= profile_form.text_field :fav_team, class: 'form-control'%>
<% end %>
<br>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
我的用户控制器
def new
@user = User.new
@user_profile = @user.build_user_profile
end
def show
@user = User.find(params[:id])
@user_profile = @user.user_profile
end
def create
@user = User.new(user_params)
if @user.save
@user.build_user_profile
@user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
render 'new'
end
end
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, user_profile_attributes: [:age, :contact, :height, :city, :state, :batting_style, :bowling_style, :fav_team, :fav_players])
end
申请日志:
Started POST "/users" for ::1 at 2015-03-31 14:38:58 -0400
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"tAFZCk9+l0ksbMSZ8OEosQP5VBRfYfvZ3RR5/tkkW3Dr16+h2Xr1m8ITt+zJvigZpaN4lIchFhPrIx81lf6wNA==", "user"=>{"name"=>"Test User", "email"=>"testuser@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "user_profile"=>{"age"=>"29", "height"=>"5 feet 4 inches", "contact"=>"888-888-8888", "city"=>"Philadelphia", "state"=>"PA", "batting_style"=>"Right hand", "bowling_style"=>"Right Arm", "fav_players"=>"Test", "fav_team"=>"Test"}}, "commit"=>"Create my account"}
Unpermitted parameter: user_profile
(0.1ms) begin transaction
User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('testuser@gmail.com') LIMIT 1
SQL (1.6ms) INSERT INTO "users" ("name", "email", "password_digest", "created_at", "updated_at", "activation_digest") VALUES (?, ?, ?, ?, ?, ?) [["name", "Test User"], ["email", "testuser@gmail.com"], ["password_digest", "$2a$10$LsY8eVTzj0CNGUp1WLiZN.DOLRbU1Qyg8W/dUHDo77YmKH099.oOa"], ["created_at", "2015-03-31 18:38:58.699583"], ["updated_at", "2015-03-31 18:38:58.699583"], ["activation_digest", "$2a$10$y7iCb2vOJzTExisD.vkYzOWxs55W.FiYZ56aGYd028s49i1j9r3su"]]
(0.8ms) commit transaction
UserProfile Load (0.5ms) SELECT "user_profiles".* FROM "user_profiles" WHERE "user_profiles"."user_id" = ? LIMIT 1 [["user_id", 120]]
Rendered user_mailer/account_activation.html.erb within layouts/mailer (0.4ms)
Rendered user_mailer/account_activation.text.erb within layouts/mailer (0.1ms)
尝试不同的方式:仍然没有运气
def create
@user = User.new(user_params)
@user_profile = @user.build_user_profile(params[:user][:user_profile])
if @user.save
@user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
render 'new'
end
end