No route matches error. Whats wrong with this code? - ruby-on-rails

I'm newbie on rails and getting an error while try to add new method on my controller :(
I have a controller under admin path;
Admin::MyUsersController < ApplicationController
before_filter :......
def index
redirect_to :action => :show_my_action
end
def show_my_action
...
...
end
My controller like this but not this exactly.
In my routes.rb
namespace "admin" do
resources :my_users do
get "show_my_action"
end
end
When my routes.rb is like this, im getting error => No route matches {:action=>"show_my_action", :controller=>"admin/my_users"}
namespace "admin" do
resources :my_users do
get "show_my_action", :on => :collection
end
end
when my routes.rb like this then no error :S
Why im getting this error. I can use first declaration for other controllers which is on root path.

You are adding actions to RESTful actions, if you don't specify a collection, or a member, the route can't know what you want. If you define like this:
namespace "admin" do
resources :my_users do
get "show_my_action"
end
end
How can routes know which route you want:
my_users/show_my_action, or my_users/:id/show_my_action
So, you need to specify it's member or collection:
namespace "admin" do
resources :my_users do
get "show_my_action", :on => :collection
end
end
will have route: my_users/show_my_action, and:
namespace "admin" do
resources :my_users do
get "show_my_action", :on => :member
end
en
will have route: my_users/:id/show_my_action
You can check at Adding More RESTful Actions.

You need to specify whether the action is on a member or a collection. If it's on a member then your URL is admin/my_users/:id/show_my_action. If it's on a collection then it's admin/my_users/show_my_action. Read up on it here: http://edgeguides.rubyonrails.org/routing.html

Related

need help on rails nesting routes

I want to get a route like
GET /example/notifications/status, to: example/notification#status
POST /example/notifications/disable to: example/notification#disable
Here is what I did
resources :example, only => %i[index] do
collection do
resources :notifications, :only => [] do
collection do
get :status
post :disable
end
end
end
end
it get the right path but it point to notification#status not example/notification#status
is there any way I can get both right path and controller expect code like
get "notifications/status", :to => "example/notifications#status"
You can use namespace for this purpose like below,
in you config/routes.rb file
namespace :example do
collection do
get :status
post :disable
end
end
It will hit the example/notification#status action.
for more information see here
Don't use resources since you don't want (aparently) any param on your routes. Go for namespace instead:
namespace :example do
namespace :notifications do
get 'status'
post 'disable'
end
end
Gives you:
$ rails routes -g example
Prefix Verb URI Pattern Controller#Action
example_notifications_status GET /example/notifications/status(.:format) example/notifications#status
example_notifications_disable POST /example/notifications/disable(.:format) example/notifications#disable

How to force to pass a required url path via get method

I have a route
collection do
get :show_logs
end
And I want the user should request show_logs/[:id].
Forbid user to send show_logs request without required id
What's the better ways to get it ?
UPDATE
If now, I wrote my rule as following,
And trying to access without :id, http://localhost:3000/tool/mvaas/relay_queries/show_logs
I'll get the error ActiveRecord::RecordNotFound in xxx
routes
get '/tool/mvaas/relay_queries/show_logs/:id', to: 'tool/mvaas/relay_queries#show_logs', :via => :get, :as => 'show_logs_tool_mvaas_relay_queries'
namespace :tool do
namespace :mvaas do
resources :relay_queries do
collection do
end
end
end
end
You should put it into member instead of collection
resources :users do
member do
get :show_logs
end
end
It will be accessible with the url /users/:id/show_logs
If you absolutely want the url to be /users/show_logs/:id then you should go with
get '/users/show_logs/:id', to: 'users#show_logs'
before your resources :users do block
You can verify if params exist by following way:
if(params.has_key?(:one))
If exist- request will done.
If absent - redirect/render or show notice.
you could try:
get "show_logs/:id" => "controller#action"
with updates: just write something like:
namespace :tool do
namespace :mvaas do
resources :relay_queries do
collection do
get "show_logs/:id", action: "show_logs"
end
end
end
end

routing is too verbose - rails

Suppose I have a model User and I want to add some dashboard namespace. So I create dashbord directory and put inside private_users_controller.rb. Now for routing I put
namespace "dashboard" do
resources :users do
member do
get "show" => "private_users#show"
end
end
end
the problem is that I only want to route the get request having this route /dashboard/users/:id/show. But rake routes shows a bunch of post, delete... routes.
How can I cut those ?
seems like you don't need any of the method from resources definition, so just add a match will be ok.
namespace "dashboard" do
match 'users/:id/show', :to => 'private_users#show'
end
if you insist using resource, then the following will work
scope '/dashboard' do
resources :users, :only => :show, :module => 'private'
end
the 'rake routes' output is like this
GET /dashboard/users/:id(.:format) private/users#show
the trailing 'show' inside the url is not needed.
namespace "dashboard" do
get "users/:id/show" => "private_users#show"
end

How to declare a rails resource with a parameter for new action?

I have a model named Entree for which the new action needs a parameter, the id of another model named Cave. I don't want to nest Entree in Cave since Cave is already nested.
What I did was declaring the resource Entree as follow in routes.rb:
resources :entrees, :except => [:new]
match "/entrees/new/:id", :to => "Entrees#new", :as => 'new_entree'
That works, but the problem is when there's an error in the create action, I want to display the page again with the invalid input. But since there's no new action, I must do a redirect_to new_entree_path, which does not keep the user input.
I have tried the following (simplest) route:
resources :entrees
But then the path http://localhost:3000/entrees/new/32 returns an error:
No route matches [GET] "/entrees/new/32"
The question is, how can I declare the Entree resource in the routes file with a parameter for the new action ?
I'm not sure if that's a hack or not, but the following works and seems cleaner than 2-levels nesting.
resources :entrees, :except => [:new] do
collection do
get 'new/:id', :to => "entrees#new", :as => 'new'
end
end
Now I can do a render "new" instead of a redirect_to.
I must say that I must have asked my question wrongly, my bad.
Rails has a route helper called path_names that does this:
resources :entrees, path_names: { new: 'new/:id' }
To improve gwik 's solution (which in fact didn't work for me):
resources :things, except: [:new] do
new do
get ':param', to: 'things#new', as: ''
end
end
It gets you new_thing_* helpers (instead of new_things_*) for free.
If you want to use Rails resource routes, you will have to nested them according to how they work
resources :caves do
resources :entrees
end
to get the route /caves/70/entrees/new
Otherwise, you are in a world of creating manual match routes.
match "/entrees/new/:id", :to => "entrees#new", :as => 'new_entrees'
I do not understand why you are forced to use a redirect? The new_entrees route is valid. You will not be able to use the form_for helper, since it is not a resource, but the form_tag helper will work.
UPDATE: Render and Route
The Route does not directly change what view is rendered in the Controller. That is determined by the controller itself. Render examples:
render :new will render the new action's view
render 'entrees/new' will render the entrees/new template
I found this generates the correct new_thing_path method not new_things_path as Antoine's solution.
resources :things, :except => [:new] do
with_scope_level(:new) do
get 'new/:param', :to => "things#new", :as => ''
end
end

Route a controller to namespace :admin to /admin

I feel like this may be a dumb question, but it's late and my head is melting a bit.. So I appreciate the assistance.
I'm trying to map the url http://localhost:3000/admin to a dashboard controller but i'm epically failing. Maybe this isn't even possible or the completely wrong idea but anyway my routes looks like this and yes
namespace :admin do
resources :dashboard, { :only => [:index], :path => '' }
...
end
and my simple dashboard_controller.rb
class Admin::DashboardController < ApplicationController
before_filter :authenticate_user!
filter_access_to :all
def index
#schools = School.all
end
end
and my view is located in views/admin/dashboard/index.html.erb
thanks for any input
If all you're trying to do is route /admin to that dashboard controller, then you're overcomplicating it by namespacing it like that.
Namespacing with a nested resource like that would mean that it would be /admin/dashboards for the :index action instead of having a clean /admin route (and you can verify that by running rake routes at the command line to get a list of your routes).
Option 1: You meant to namespace it like that
# putting this matched route above the namespace will cause Rails to
# match it first since routes higher up in the routes.rb file are matched first
match :admin, :to => 'admin/dashboards#index'
namespace :admin do
# put the rest of your namespaced resources here
...
end
Option 2: You didn't mean to namespace it like that
Route:
match :admin, :to => 'dashboards#index'
Controller:
# Remove the namespace from the controller
class DashboardController < ApplicationController
...
end
Views should be moved back to:
views/dashboards/index.html.erb
More info: http://guides.rubyonrails.org/routing.html
Regarding to http://guides.rubyonrails.org/routing.html I prefer this
namespace :admin do
root to: "admin/dashboards#index"
resources :dashboard
end
Try this:
namespace :admin do
root to: 'users#index' # whatever. Just don't start with /admin
#resources :dashboards <= REMOVE THIS LINE !
end

Resources