【发布时间】: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"> </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