【问题标题】:Rendering Devise edit form as partial on another view with user data使用用户数据在另一个视图上渲染设计编辑表单
【发布时间】:2023-03-04 22:36:01
【问题描述】:

我想在我的应用程序的模式中触发devise/registrations/edit.html.erb

现在,我一切正常,除了当模式被触发时,我在表单字段中看不到 current_user 信息。

当我转到/settings(这是我的应用程序中users/edit 的路线)时,它会正确显示current_user 信息。

但当我在应用程序的模式中看到它时却没有。

我所做的是基于编辑创建了一个部分,现在正在渲染该部分。

所以这是部分:devise/registrations/_edit.html.erb:

<h2>Edit <%= resource_name.to_s.humanize %></h2>

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :email, :required => true, :autofocus => true %>
    <%= f.input :name, :autofocus => true %>
    <%= f.input :avatar %>

    <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
      <p>Currently waiting confirmation for: <%= resource.unconfirmed_email %></p>
    <% end %>

    <%= f.input :password, :autocomplete => "off", :hint => "leave it blank if you don't want to change it", :required => false %>
    <%= f.input :password_confirmation, :required => false %>
    <%= f.input :current_password, :hint => "we need your current password to confirm your changes", :required => true %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Update" %>
  </div>
<% end %>

<%= link_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete %>

<%= link_to "Back", :back %>

这是它被渲染的地方:

<div id="overlay">&nbsp;</div>
<div class="popup center-block" id="edit_profile">
  <div class="titles clearfix">
      <!-- <h2>Edit Account</h2> -->
            <h2>Edit <%= resource_name.to_s.humanize %></h2>            
  </div>
  <div class="content">

        <%= render "devise/registrations/edit" %>

  </div>
</div>

它在我的Dashboard#Index 上呈现,所以这就是我的dashboard_helper.rb 的样子:

module DashboardHelper
  def resource_name
    current_user.name || :user
  end

  def resource
    # @resource ||= User.new
    @resource ||= current_user
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end

如何让编辑表单显示在模态中以显示current_user 信息?

编辑 1

在我看来,我什至尝试过:

<%= render "devise/registrations/edit", locals: { resource: current_user, :resource_name => User }  %>

但它仍然没有工作。

编辑 2

当我使用pry 进行调试时,它显示resource 变量为空 - 这是来自devise/registrations/_edit.html.erb

[1] pry(#<#<Class:0x007fedf8be85c8>>)> resource_name
=> :user
[2] pry(#<#<Class:0x007fedf8be85c8>>)> resource
=> #<User id: nil, email: "", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, name: nil, confirmation_token: nil, confirmed_at: nil, confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: nil, avatar: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_id: nil, invited_by_type: nil, invitations_count: 0, bio: nil>

看来 DashboardHelper 覆盖不起作用。

【问题讨论】:

  • 此问题已成功回答here

标签: ruby-on-rails ruby-on-rails-4 devise


【解决方案1】:

我很不清楚在帮助器中定义resource 并将其传递给部分会产生什么结果。需要明确的是,如果您将其更改为:

module DashboardHelper

  #...
  
  def resource
    @resource ||= current_user
  end

  #...

end

另外,将 render 声明为 &lt;%= render "devise/registrations/edit" %&gt;(没有本地变量)

【讨论】:

  • 不。不工作。我用 pry 的输出更新了问题。
  • resourcedevise/registrations/_edit.html.erb 的输出是什么?
  • 以上是该部分的输出。那就是我放binding.pry 的地方。在那个部分。
  • 嗯,所以它只是 User.new。从您在上面发布的唯一一点可能导致它的原因是@resource ||= User.new(而不是@resource ||= current_user)。我假设您已登录并且您的 current_user 指向现有用户。
  • 你的假设是正确的。我还更新了问题以更好地反映当前的dashboard_helper.rb。我接受了你的建议并以此为基础。这就是结果。此外,在同一部分,我有以下代码:&lt;h2&gt;Edit &lt;%= current_user.name %&gt;&lt;/h2&gt; &lt;%= resource_name.to_s.humanize %&gt;,在模式中,它准确地打印出用户名,如:“Edit Marcamillion”换行符:“User”。其中“用户”对应于resource_name.to_s.humanize
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
  • 2020-06-29
相关资源
最近更新 更多