我正在尝试在我的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
在我的ApplicationController中:
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
Tutor Load(3.2ms)SELECT“tutors”。* FROM“tutors”WHERE“tutors”。“user_id”=?订购“tuto rs“。”id“ASC LIMIT 1 [[”user_id“,1]]
知道我可能做错了吗?
谢谢!
答案 0 :(得分:2)
这可能是因为没有与用户连接的导师关联。如果导师是零,则字段不会显示。
如果是这样,您需要弄清楚如何在用户身上执行此操作。
@user.build_tutor
添加此选项的一些选项 - 您可能需要覆盖设计控制器/方法以添加此选项。设计可能会提供一些钩子。您可以在视图中执行此操作。