In my routes.rb I have this to add "settings" as an additional action to insurances:
namespace :modules do
namespace :insurance do
resources :insurances do
member do
get :settings
end
end
end
end
According to "rake routes" this gives the following path:
settings_modules_insurance_insurance_path
But when I visit that path in the browser, it returns the error:
No route matches {:action=>"settings", :controller=>"modules/insurance/insurances"}
This is the full ouput of rake routes:
settings_modules_insurance_insurance GET /modules/insurance/insurances/:id/settings(.:format) {:action=>"settings", :controller=>"modules/insurance/insurances"}
What should I do?
You've put the new route on member, so you have to pass the id of the insurance:
settings_modules_insurance_insurance_path(#insurance)
The full name of the controller should be Modules::Insurance::InsurancesController, right?
Related
Please tell me what will be the url helpers for the following code?
scope module: 'admin' do
resources :articles, :comments
end
and
scope '/admin' do
resources :articles, :comments
end
and
namespace :admin do
resources :articles, :comments
end
As per the rails guide document here - Controller namespace and routing
1.
If you want to route /articles (without the prefix /admin) to Admin::ArticlesController, you can specify the module with a scope block
the path will be like
GET articles_path #index action
GET comments_path #index action
If instead, you want to route /admin/articles to ArticlesController (without the Admin:: module prefix), you can specify the path with a scope block:
this will give the following path but the controller will contain Admin:: prefix
GET admin_articles_path # index action
GET admin_comments_path #index action
with namespace, the route will prefix by admin as well as controller needs to have Admin:: module prefix
GET admin_articles_path # index action
GET admin_comments_path #index action
Running the following commands on a console will return the available routes for your application.
rails routes | grep article
rails routes | grep comment
I am trying to make admin route with namespace but it doest trigger to the route
I run rails g controller admin
it created the file app/controllers/admin_controller.rb , app/views/admin/index.html.haml
my namespace look like this
namespace :admin do
controller :admin do
get '/', :index
end
end
it doesn't trigger to localhost:3000/admin it said not found for that route
any idea ??
namespace not only adds a path prefix but it also adds a module nesting to the expected controller. For example:
namespace :blog do
resources :posts
end
Will create the route /blog/posts => Blog::PostsController#index.
In your example Rails expects the controller to be defined as:
# app/controllers/admin/admin_controller.rb
module Admin
class AdminController
# GET /admin
def index
end
end
end
If you just want to add a path prefix or other options to the routes without module nesting use scope instead:
scope :admin, controller: :admin do
get '/', action: :index
end
This will route /admin to AdminController#index
But if this just a one off route you could just write:
get :admin, to: 'admin#index'
Your controller path needs to be in the admin namespace.
So the filename for the admin controller needs to be app/controllers/admin/admin_controller.rb.
In Rails 5 I've figured out how to
Overwrite the route parameter from id to something like name
Add another route for a resource
So that my routes.rb looks something like this
Rails.application.routes.draw do
resources :cats, param: :name
resources :cats do
get :preview, on: :member
end
end
I've noticed however that my additional preview route does not keep the overwritten named route parameter. Instead, when looking at the output from rake routes, I have something that looks like this.
GET /cats/:id/preview(.:format)
when what I was expecting, and trying to achieve, was a route that looks like
GET /cats/:name/preview(.:format)
How do I both add an additional route to a resource while overwriting the parameter?
You're duplicating your routes entries for cats, and you've provided the block for declaring the preview route on the entry missing the param name override. You need to provide the override and the block in the same route declaration.
Rails.application.routes.draw do
resources :cats, param: :name do
get :preview, on: :member
end
end
This gives you the route you want:
$ rake routes
Prefix Verb URI Pattern Controller#Action
preview_cat GET /cats/:name/preview(.:format) cats#preview
I have an AppleController
it has a def sliceme method
when I go to: /apple#sliceme
it routes to #index
In my routes.config I have
resources :apples
Why?? And what is the correct route??
Resources will create the CRUD method routes (see here)
If you want to specificity another route you can specify it like so in your routes file:
get "apple/sliceme", to: "apple#sliceme"
Or
resources :apple do
get :sliceme, on: :collection
end
To check what routes actually exist, run rake routes in the terminal
I am trying to add a customer action to one of my resources, therefore I created a custom route:
namespace :admin do
resources :subscriptions
match 'subscriptions/summary', :to => 'subscriptions#summary', :as => 'subscriptions_summary'
end
In my rake routes I'm getting the following output:
admin_subscriptions_summary /admin/subscriptions/summary(.:format) spree/admin/subscriptions#summary
The problem now is, whenever I try to create a link to the summary action, I get following error:
Missing template spree/admin/subscriptions/show
Why is my app confusing the show action with the summary action?
namespace :admin do
resources :subscriptions do
collection do
get 'summary'
end
end
end
solved it.