0

Im setting up a Landlord Management System and trying to manage other users. I currently have admin as a boolean attribute in my users model, so i would like to edit other users and make them admin via the edit form.

I am trying to do this via influence from this post Edit other user as admin in devise, ruby on rails

So Here is my code:

Routes.rb

devise_for :users, :path_prefix => 'my'
resources :users

views\users\edit

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

<%= form_for(@user) do |f| %>
<%= f.error_notification %>

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

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

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

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

<h3>Cancel my account</h3>

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

controllers\users

class UsersController < ApplicationController

def edit
@user = User.find(params[:id])
end


def update
@user = User.find(params[:id])
if @user.update(user_params)
redirect_to adminpanel_path
else
render 'edit'
end
end

end

Issue

if i go to http://localhost:3000/users/2/edit to edit a user i get the following error undefined local variable or method resource_name for #<#<Class:0x8b9df40>:0x8ba7360>

Kush
  • 31
  • 6
  • Why do you have `devise_for :users` and `resources :users`? – hashrocket Feb 11 '19 at 21:03
  • please see answer in the following question https://stackoverflow.com/questions/35583473/edit-other-user-as-admin-in-devise-ruby-on-rail, this is what i was trying to implement – Kush Feb 11 '19 at 21:05
  • It looks like the `views\users\edit` code you show is the Devise code. Is that correct? If so, you seem to be mixing up the devise code and the users code you need. – hashrocket Feb 11 '19 at 21:11
  • hi @hashrocket yes that is correct that is what it said on previous post mentioned in question. https://stackoverflow.com/questions/35583473/edit-other-user-as-admin-in-devise-ruby-on-rail, but i did change line 3. – Kush Feb 11 '19 at 21:15

1 Answers1

0
undefined local variable or method resource_name for #<#<Class:0x8b9df40>:0x8ba7360>

tells you that resource_name is undefined. Remove all resource references from your form and it will work. As for your cancel account link:

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

you need to change resource_name to the user id you want to delete.

I've also seen you are using custom attributes for devise:

<%= f.input :admin,
            required: false %>

have you permitted them? Otherwise, this will be your next error. If you don't know how to do that, comment quickly and i'll update my answer.

Greetings!

Prometheus
  • 799
  • 8
  • 28