我有以下User
具有artist_attributes
虚拟属性的模型:
class User < ActiveRecord::Base
attr_accessor :artist_attributes
attr_accessible :artist_attributes
belongs_to :artist
accepts_nested_attributes_for :artist, update_only: true
end
这是艺术家模型:
class Artist < ActiveRecord::Base
has_one :user
end
问题是没有设置虚拟属性。例如,这里是我的用户控制器:
class Members::UsersController < Members::BaseController
def update
current_user.artist_attributes = params[:user].delete(:artist_attributes)
p current_user.artist_attributes # => nil
# ...
end
end
这是提交的表格:
<%= simple_form_for current_user, url: members_profile_path do |f| %>
<%= f.simple_fields_for :artist do |ff| %>
<%# ... %>
<% end %>
<%= f.submit "Edit profile", class: "btn btn-primary", disable_with: "Editing profile..." %>
<% end %>
为什么没有设置artist_attributes
虚拟属性?是因为嵌套形式吗?
这是我提交表单后的参数哈希:
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"XQI4y7x7CSjxUhdYvEv2bLEjitwCfXxeTBUU3+kYS4g=",
"user"=> {
"email"=>"youjiii@gmail.com",
"forename"=>"You",
"surname"=>"Jiii",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"artist_attributes" => {
"bio"=>"NERVO is awesome!",
"phone_number"=>"",
"city"=>"",
"country"=>"Afghanistan"
}
},
"commit"=>"Edit profile"}