【问题标题】:Rails 4: nested attributes not displaying with DeviseRails 4:嵌套属性不与 Devise 一起显示
【发布时间】:2013-07-30 21:08:30
【问题描述】:

我正在尝试在我的 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 限制 1

Tutor Load (3.2ms) SELECT "tutors".* FROM "tutors" WHERE "tutors"."user_id" = ? ORDER BY "tuto rs"."id" ASC 限制 1 [["user_id", 1]]

知道我做错了什么吗?

谢谢!

【问题讨论】:

  • resource 设置为什么?是新用户吗?
  • 这是 Devise 的默认设置。这是我想象的当前用户会话的形式,但我不完全确定。

标签: ruby-on-rails model nested-attributes


【解决方案1】:

这可能是因为没有连接到用户的导师协会。如果导师为零,则字段不会显示。

如果是这样,您需要弄清楚如何对用户执行此操作。

@user.build_tutor

添加此选项的一些选项 - 您可能需要覆盖设计控制器/方法才能添加此选项。可能有一些设计提供的钩子。您可以在视图中执行此操作。

【讨论】:

  • 我添加了 这似乎显示了字段集,但是当我更新表单时,文本没有被存储并且每次我尝试更新时都会消失。顺便提一下,current_user 是 Devise 提供的。
  • 在视图中,您可以先寻找导师,然后再建立新导师。 (current_user.build_tutor 除非 current_user.tutor)。理想情况下,这将在控制器中完成,但您可以在字段显示在表单中之前的任何地方进行。
  • 我在视图中尝试过,但它中断了(页面甚至没有显示)
  • 我不认为你想对'current_user'(对不起我上面的评论)这样做,因为它与表单中的用户不同。您要么希望将其添加到资源或 f.object。当您提交时 - 它应该自然地将导师对象添加到用户,并且您将在第二次显示页面时在用户上拥有一个导师对象。
  • current_user 将点击 devise 方法并获取当前登录的用户。在 'new' 方法中,这可能是巧合的同一用户,但在发布后它将是根据帖子的属性创建的用户。它将执行 User.new(params[:user]) 并且不再与 current_user 相关。
猜你喜欢
  • 1970-01-01
  • 2018-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多