Handling User Management using Devise

Hi,

I have been working using devise in ROR and currently creating an app that allows both admins and users can use it but with different roles and access to the app. It is currently in a prototyping phase and not complete yet. The idea here is that the admin can create users and change it’s roles and access information on the go, but the user can also change their information once they login. At the moment I am trying to add users into the database using admin login via devise into PostgreSQL. The issue I am having is that one I cannot view the list of users that I added earlier before, and the strange part is that I cannot view the add new user button on the user’s index page, and secondly I am having an issue to add the new user to the database via new user form.

I think that there might be some issue to the routes or the code. Kindly please help me in this matter.

Regards,

Asim Khan

routes

Please find attached other screenshots to the code. show_page show_user

user_index

Here are the last set of the screenshot. user_model

user_routes

Your routes file is specifying the users routes wrong:

  • You’re using “resource” instead of “resources” (the cause of your index problem)
  • You’re leaving out the “create” action (the cause of your 404 POST)
  • You’re including “update” but not “edit”

Generally, I think you want all the actions, so just remove the “only” option.

namespace :admin do
  resources :users
end

Thank you so much that worked, the entry was successful and the redirection was possible. But when I try to click on a certain user to view the information to edit. I encounter an error on the delete button. It is throwing an error but I do not understand.

Regards, Asim Khan click_show_user delete_user_error

Ok, so I have fixed the issue just updated the path for destroy user with admin_user_path(@user).

But now I am facing another unusual error when I try to update the record, I get the following error which is not makin sense, even though I have made all the necessary code.

Regards, Asim Khan

patch_error

PATCH updates an existing record but the URL in the record doesn’t include the record ID, it should be /admin/users/some_id. Something is off either with your routes or your model ID.

All the issues you’re having are very basic. I’d suggest talking them through with ChatGPT who can provide more elaborate and faster responses.

I replaced this line of code

<%= form_with(model: @user, url: admin_users_path) do |form| %>

with this

<%= form_with(model: @user, url: @user.new_record? ? admin_users_path : admin_user_path(@user), method: @user.new_record? ? :post : :patch) do |form| %>

This fixed and resolved the issue.

Regards, Asim Khan