Rails 3 namespace routing


I have an admin namespace and a scaffold of companies hosted from the admin namespace. I wanted to put companies in the admin namespace. Then I put companyscontroller in the admin directory and changed the definition to class Admin::CompaniesController < Admin::AdminController and put the company views in the admin directory in / app / views / and put company_helper in the admin directory, and now it looks like this:

  module Admin :: CompaniesHelper
 end

Namespace in routes.rb:

  namespace: admin do
     root: to => "companies # index"
     resources: companies
   end

When I switch to localhost: 3000 / admin, I get this error:

  undefined method `company_path 'for #: 0xb696b408>

Now please tell me how to edit links so that the links work correctly?

+4
source share
3 answers

When you moved the controller to the admin namespace, you changed the routes to the links created in the lining templates. For example, if your templates use company_path, the links will change to admin_company_path.

To view the routes in your application at any given time, run "rake routes" from the command line at the root of your rails application. This will show you all the routes in your application.

+4
source

Since the company is under the namespace administrator, you must prefix the path with the administrator.

Same:

 admin_company_path(@company) 

Refer to the Rails guide for more information on Rails routing and namespaces.

+2
source

I got a kind of ugly solution, but it works. I created a new scaffold, but in a different way: rails generate scaffold Admin::Companies instead of rails generate scaffold Companies but I still don’t understand how helpers make a url for resources: (

0
source

Source: https://habr.com/ru/post/1344251/


All Articles