Jak używać szyn i simple_form dla zagnieżdżonych zasobów?
Próbuję utworzyć jeden zasób z innym zagnieżdżonym zasobem w tym samym czasie. Używam Rails4 i simple_form 3.0.0rc. Oto mój kod.
Modele:class User < ActiveRecord::Base
has_one :profile
accepts_nested_attributes_for :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
end
Kontroler:class UsersController < ApplicationController
def new
@user = User.new
@user.build_profile
end
def create
user = User.new user_params
user.save
redirect_to root_url
# @par =params
end
private
def user_params
params.require(:user).permit(:email, profile_attributes: [:name])
end
end
Widok (formularz dla nowego użytkownika)<%= simple_form_for @user do |f| %>
<%= f.input :email %>
<%= simple_fields_for :profile do |p| %>
<%= p.input :name %>
<% end %>
<%= f.submit %>
<% end %>
Kiedy przesyłam formularz,create
akcja odbiera toparams
:
{"utf8"=>"✓",
"authenticity_token"=>"dJAcMcdZnrtTXVIeS2cNBwM+S6dZh7EQEALZx09l8fg=",
"user"=>{"email"=>"[email protected]"},
"profile"=>{"name"=>"Vasily"},
"commit"=>"Create User",
"action"=>"create",
"controller"=>"users"}
I po wywołaniuuser_params
jedyną rzeczą, która pozostała, jest
{"email"=>"[email protected]"}
I, jak widać, nie ma w tym nicprofile
, więc żaden profil nie zostanie utworzony.
Co ja robię źle?