我正在尝试在我的 Devise edit.html.erb 文件中包含嵌套属性。
我的模型:
class User < ActiveRecord::Base
has_one :tutor, dependent: :destroy
accepts_nested_attributes_for :tutor, allow_destroy: true
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
end
和
class Tutor < ActiveRecord::Base
belongs_to :user
end
在我的应用程序控制器中:
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protect_from_forgery with: :exception
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) do |u|
u.permit(:first_name, :last_name, :email,
:is_tutor, :password,
# This is important for nested attributes
tutor_attributes: [:id, :_destroy, :user_id, :description]
:password_confirmation, :current_password)
end
end
end
而我的观点:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :multipart => true }) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div><%= f.label :first_name %><br />
<%= f.text_field :first_name %></div>
<div><%= f.label :last_name %><br />
<%= f.text_field :last_name %></div>
<p>
<%= f.check_box :is_tutor %>
<%= f.label :is_tutor, 'Tutor' %>
</p>
<div>
<%= f.fields_for :tutor do |builder| %>
<fieldset>
<%= builder.label :description, 'Description' %><br />
<%= builder.text_area :description %><br />
<%= builder.check_box :_destroy %>
<%= builder.check_box :_destroy, 'Remove description' %>
</fieldset>
<% end %>
</div>
<br />
<div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, :autocomplete => "off" %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password %></div>
<div><%= f.submit "Update" %></div>
<% end %>
当我进入编辑页面时,我没有看到带有嵌套属性的字段集。对 db 的查询如下所示:
用户负载 (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" A SC LIMIT 1
导师负载(3.2 毫秒) SELECT "tutors".* FROM "tutors" WHERE "tutors"."user_id" = ? ORDER BY "tuto rs"."id" ASC LIMIT 1 [["user_id", 1]]
知道我可能做错了什么吗?
谢谢!