Malek Zalfana
Malek Zalfana

Reputation: 318

Updating user information outside devise pages

I want to add the option for the user to edit his email/password/image... from the /settings page not from the /users/edit page. So I copied the code to the settings page

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
  <%= devise_error_messages! %>
  <div class="field">
    <%= 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 class="form-group">
    <%= f.label :image %>
    <%= f.file_field :image, class: 'form-control'%>
  </div>

  <div class="form-group">
    <%= f.label :cover %>
    <%= f.file_field :cover, class: 'form-control'%>
  </div>

  <div class="field">
    <%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
    <%= f.password_field :current_password, autocomplete: "off" %>
  </div>

  <div class="actions">
    <%= f.submit "Update" %>
  </div>
<% end %>

<h3>Cancel my account</h3>

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

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

but it returned this

undefined local variable or method `resource' for #<#<Class:0x007f60245ab9b0>:0x007f6026f58358>

What should I add to my controller?

Upvotes: 1

Views: 425

Answers (1)

Sravan
Sravan

Reputation: 18647

When ever you want to perform any custom actions, first create an action in a controller, then a route to it and then the view file.

resource object is only available in devise routes.

So, first add a method named settings in any controller eg: say userscontroller.

class UsersController < ApplicationController
    def settings
        @user = current_user
    end
end

Then in routes.rb, add a route which points to this action with a route named '/settings'

get '/settings' => 'users#settings', as: :settings

Now, in app/views/users/settings.html.erb

<%= form_for(@user, url: registration_path(@user), html: { method: :put }) do |f| %>
      <%= devise_error_messages! %>
      <div class="field">
        <%= 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 class="form-group">
        <%= f.label :image %>
        <%= f.file_field :image, class: 'form-control'%>
      </div>

      <div class="form-group">
        <%= f.label :cover %>
        <%= f.file_field :cover, class: 'form-control'%>
      </div>

      <div class="field">
        <%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
        <%= f.password_field :password, autocomplete: "off" %>
      </div>

      <div class="field">
        <%= f.label :password_confirmation %><br />
        <%= f.password_field :password_confirmation, autocomplete: "off" %>
      </div>

      <div class="field">
        <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
        <%= f.password_field :current_password, autocomplete: "off" %>
      </div>

      <div class="actions">
        <%= f.submit "Update" %>
      </div>
    <% end %>

<h3>Cancel my account</h3>

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

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

So, in the show page of user add a link to settings page,

<%= link_to 'Edit', settings_path(user) %>

Upvotes: 1

Related Questions