渲染设计编辑表单作为部分在另一个视图上与用户数据

时间:2014-10-23 20:12:36

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

我想在我的应用中以模式触发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覆盖不起作用。

1 个答案:

答案 0 :(得分:3)

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

module DashboardHelper

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

  #...

end

另外,将渲染声明为<%= render "devise/registrations/edit" %>(没有本地人)