3

I want to fire the devise/registrations/edit.html.erb in a modal in my app.

Right now, I have everything working, except that when the modal is fired I don't see the current_user information in the form fields.

When I go to /settings (which is the route for users/edit in my app), it shows the current_user information correctly.

But it doesn't when I see it in the modal within the app.

What I did was created a partial based on the edit, and am now rendering that partial.

So this is the partial: 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 %>

This is where it gets rendered:

<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>

It gets rendered on my Dashboard#Index, so this is what my dashboard_helper.rb looks like:

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

How do I get the edit form displayed in the modal to show the current_user info?

Edit 1

I even tried this in my view:

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

But it still hasn't worked.

Edit 2

When I use pry to debug this, it shows me that the resource variable is empty - this is from the 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>

So it seems that the DashboardHelper override is not working.

marcamillion
  • 32,933
  • 55
  • 189
  • 380
  • This question has been successfully answered [here](https://stackoverflow.com/questions/4081744/devise-form-within-a-different-controller?rq=1). – pSkarl Jul 20 '18 at 09:15

1 Answers1

3

It is quite unclear to me what would be the result of having resource defined within a helper and also passed to a partial. To be clear, what if you change it to:

module DashboardHelper

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

  #...

end

Also, declare render as <%= render "devise/registrations/edit" %> (without locals)

blelump
  • 3,233
  • 1
  • 16
  • 20
  • Nope. Doesn't work. I updated the question with output from pry. – marcamillion Oct 23 '14 at 20:38
  • What's the output of `resource` from `devise/registrations/_edit.html.erb` ? – blelump Oct 23 '14 at 20:41
  • The above is the output from that partial. That's where I put the `binding.pry`. In that partial. – marcamillion Oct 23 '14 at 20:53
  • Well, so it's simply User.new. From what you've posted above the only point what may cause it, is `@resource ||= User.new` (instead of `@resource ||= current_user`). I assume you're logged in and your current_user points to an existing user. – blelump Oct 23 '14 at 21:00
  • Your assumption would be correct. I also updated the question to better reflect the current `dashboard_helper.rb`. I took your suggestion and built upon it. This is the result. Also, on that same partial, I have the following code: `

    Edit <%= current_user.name %>

    <%= resource_name.to_s.humanize %>` and in the modal, it accurately prints out the user name like: "Edit Marcamillion" newline: "User". where "User" corresponds to `resource_name.to_s.humanize`.
    – marcamillion Oct 23 '14 at 21:10
  • You may try to drop `resource` method and see if it's complain that `resource method cannot be found`. If so, then I cannot see any other options. If it's not however, then it is declared somewhere else and that's the point. – blelump Oct 23 '14 at 21:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63564/discussion-between-marcamillion-and-blelump). – marcamillion Oct 23 '14 at 21:32