Rails How to Use Associated Model with the Admin Namespace

Create Devise User from Admin Namespace in Rails 5.1

using the registration#create action is the appropriate architecture for this?

Because that action is built on a different concept, that the user is not logged in and there are a series of checks for that.

views/admin/users/new.html.erb

you are using registration#new controller action, which does not have the @admin valorized.

def new
@admin = Admin.find(params[:admin_id])
....
end

That is why you get the error, also this approach is not correct.
User will need to register with the traditional devise router, then you will also have to create a nested router for admins to create new users.

this is your link to the admin user registration#new
Your current view/controller#action has the @admin = Admin.find(params[:id]) valorized and the view has the following link

link_to new_admin_user_path(:admin, :user)

the new_admin_user_path is for url /admin/:admin_id/users/sign_up(.:format) that you define in your nested routes.

Step 2) decide which controller#action to use

Do you want to use the standard users/registrations#create or use a new action for this?

I believe you should enhance users/registrations#create by generating the controllers actions in your app as described in devise guide

then in devise controller registrations

def new 
@admin = Admin.new(:params[:admin_id]) if params[:admin_id].present?
end

your registrations#new and #create will still trigger errors, you will have to read how devise creates this users by reading their amazing sourcecode and rdoc documentation, modify the process accordingly so that admins can create users by using that action otherwise the less DRY alternative is creating a new controller#action and using it to call an existing User method or a method you will create in the User model to create users. In the end it a User is just an entry in your users database table. Just creating in a similar fashion to other users. As the admin is creating the temporary password, encryption/security issues are not anymore that important. The User will have to change the password anyway.

adding controllers with a namespace admin as a subfolder

You seem to not have an understanding of how Rails's RESTful routing works by default. I recommend reading the Resource Routing section of the Rails Guides. By default, when using resources in your routes, the show action is what is used to display a particular model record. You can customize this behavior to an extent in that you can change the URL that for the show action, but not the method name in the model:

resources :users, :path_names => { :new => 'list' }

If you are going to use RESTful routing (which you should), you should remove the default route (match ':controller(/:action(/:id))(.:format)'). Also, you can run rake routes at any time from the terminal to see details about your current routing configuration.

deny acces namespace ability in ruby on rails

I think it's more common to do that in controllers.

In my case, I use before_action in the AdminBaseController, which all admin related controllers inherited from, to validate if user is logged in as an admin.

class AdminBaseController < ApplicationController
before_action :authenticate_admin_user!

def authenticate_admin_user!
# authentication related logic goes here
redirect_to root_url unless current_user.try(:admin?)
end
end

redirect_to object in admin namespace

You can use polymorphic_path

polymorphic_path([:admin, @attachment.owner])

Ruby on Rails model inside namespace can't be found in controller

Try:

@blog_entries = ::Blog::Entry.find(:all)

It's currently looking for the wrong class. Using :: before Blog will force it to look from the top level.



Related Topics



Leave a reply



Submit