controller#index method showing routing error - ruby-on-rails

i'm new to ruby on rails.
I've set up a resources directive in the routes file.
resources :employees
Which creates the following routes
root / employees#index
employees GET /employees(.:format) employees#index
POST /employees(.:format) employees#create
new_employee GET /employees/new(.:format) employees#new
edit_employee GET /employees/:id/edit(.:format) employees#edit
employee GET /employees/:id(.:format) employees#show
PUT /employees/:id(.:format) employees#update
DELETE /employees/:id(.:format) employees#destroy
But when I put /employees in to the address bar I get
No route matches {:controller=>"employees", :action=>"show"}
Even though /employees should call employees#index not employees#show
Wham am i missing here?
Thanks :)

Maybe you have a link in the layout or template like employee_path (not employees_path).
And you have got this error.

Related

Rails - start your route from second element of the path

Is there a way in rails to start routing from 2nd part of the url path?
for example localhost:3000/tenant_name/posts for resources:posts
tenant_name is a name of the schema in my database.i want switch to respective tenant using the tenant_name.
when i run this now will get No route matches [GET] "/tenant_name/posts"
I need to visit posts even if replace "tenant_name"with any tenant_name.How to do?
Using scope without any options and only a scope name, it will just change the resources path.
scope :sometext_here do
resources :posts
end
This will generate url like -
Prefix Verb URI Pattern Controller#Action
posts GET /sometext_here/posts(.:format) posts#index
POST /sometext_here/posts(.:format) posts#create
post GET /sometext_here/posts/:id(.:format) posts#show
PATCH /sometext_here/posts/:id(.:format) posts#update
PUT /sometext_here/posts/:id(.:format) posts#update
DELETE /sometext_here/posts/:id(.:format) posts#destroy
Alternative way to use it -
get '/:dynamic_text/posts' => 'posts#index', as: :all_posts
So it can be used as
all_posts_path(dynamic_text: "sometext_here")

Rails routes config thinks action method is an object id

I'm developing rails application and encountered such problem.
I have movies_controller.rb, where I have these actions and routes defined:
Prefix Verb URI Pattern Controller#Action
movies GET /movies(.:format) movies#index
POST /movies(.:format) movies#create
new_movie GET /movies/new(.:format) movies#new
edit_movie GET /movies/:id/edit(.:format) movies#edit
movie GET /movies/:id(.:format) movies#show
PATCH /movies/:id(.:format) movies#update
PUT /movies/:id(.:format) movies#update
DELETE /movies/:id(.:format) movies#destroy
root GET / redirect(301, /movies)
movies_by_director GET /movies/by_director(.:format) movies#by_director
But when I try to go to /movies/by_director?director="something", rails think, that I'm navigating to movies#show action with parameter :id = by_director.
What am I doing wrong?
Routes are matched in the order they are specified so make sure the route for "by_director" is defined above the resource routes for movies.
Something like this should do the trick:
get '/movies/by_director' => 'movies#by_director'
resources :movies
There are two problems in one here:
The default pattern matching for :id is loose enough that by_director is interpreted as an :id.
Routes are matched in order and GET /movies/:id appears before GET
/movies/by_director.
You can manually define GET /movies/by_director before your resources :movie as infused suggests or you could add a constraint to narrow what :ids look like:
resources :movies, constraints: { id: /\d+/ } do
#...
end
Manually ordering the routes is fine if there's just one or two of them to deal with, constraining :id is (IMO) cleaner and less error prone.

Create routes for an admin area

This is causing me such grief!
Let's say I have a story class.
A normal user can create a story, view their stories and delete a story belonging to them.
An admin user can create a story, view all stories and delete all stories.
I want an admin area in the url structure:
/control_panel/stories # list all stories in the site
/control_panel/stories/new # create a new story
/control_panel/stories/:id # show, edit, update, delete story (via different request methods)
However, I also want users to have these routes:
/stories
/stories/new
/stories/:id
I have no idea how to implement this. Trying to create the routes has been a nightmare. This configuration:
resources :stories
scope '/control_panel' do
resources :stories
end
Is almost there:
stories GET /stories(.:format) stories#index
POST /stories(.:format) stories#create
new_story GET /stories/new(.:format) stories#new
edit_story GET /stories/:id/edit(.:format) stories#edit
story GET /stories/:id(.:format) stories#show
PATCH /stories/:id(.:format) stories#update
PUT /stories/:id(.:format) stories#update
DELETE /stories/:id(.:format) stories#destroy
GET /control_panel/stories(.:format) stories#index
POST /control_panel/stories(.:format) stories#create
GET /control_panel/stories/new(.:format) stories#new
GET /control_panel/stories/:id/edit(.:format) stories#edit
GET /control_panel/stories/:id(.:format) stories#show
PATCH /control_panel/stories/:id(.:format) stories#update
PUT /control_panel/stories/:id(.:format) stories#update
DELETE /control_panel/stories/:id(.:format) stories#destroy
However, where are my named routes for the control_panel routes?! I expected:
GET /control_panel/stories to have the name control_panel_stories (like its corresponding non-namespaced routes /stories),
GET /control_panel/stories/new to have the name new_control_panel_story
GET /control_panel/stories/:id/edit to have the name edit_control_panel_story
and
GET /control_panel/stories/:id to have the name control_panel_story
Instead I have no named routes!
Questions:
I need guidance, so tell me what I should be doing. If any of the following are irrelevant just say!
How do I give my scoped routes names? If giving them names is impossible, how do I access them in the views?
If I use namespace :control_panel instead of scope 'control_panel', I get my named routes:
resources :stories
namespace :control_panel do
resources :stories
end
gives me:
control_panel_stories GET /control_panel/stories(.:format) control_panel/stories#index
POST /control_panel/stories(.:format) control_panel/stories#create
new_control_panel_story GET /control_panel/stories/new(.:format) control_panel/stories#new
edit_control_panel_story GET /control_panel/stories/:id/edit(.:format) control_panel/stories#edit
control_panel_story GET /control_panel/stories/:id(.:format) control_panel/stories#show
PATCH /control_panel/stories/:id(.:format) control_panel/stories#update
PUT /control_panel/stories/:id(.:format) control_panel/stories#update
DELETE /control_panel/stories/:id(.:format) control_panel/stories#destroy
...however, now they don't link to the same controller anymore! Gah! They now link to a stories controller that should be found at app/controllers/control_panel/stories_controller.rb
So does this mean I should be using two controllers to edit one resource? This appears to be where rails is pushing me. Every resource has an 'open' controller and a 'control_panel' controller. Is this the correct way to do it?
I just feel like it's a waste when the 'open' controller's actions are identical to the 'control_panel' controller's actions. However, is the DRY of having one controller offset by the sheer complexity of pushing admins and normal users through one controller? This is a simple example, but I can imagine it could get a lot lot trickier. I hope this is the case :)
So what is the point of scoped routes then? They seem kind of pointless.
Do you think a namespace should be created whenever you're creating an exclusive 'area' in your site? A 'forums' area, a 'users' area, and yes, an 'admin' area? This would help to keep controllers more organised.
about routes sope and namespace, you should have a look at this article
Try with:
scope :path => 'control_panel', :as => 'control_panel' do
For example you want the admin to be able to destroy, but not a regular user:
def destroy
return redirect_to(:back, :notice => 'You need to be admin to delete.') if current_user.regular?
...
end

How to pass and receive parameters in url in Ruby on Rails?

I am new to ruby, i would like to pass parameters in the url and receive it in controller.
I am expecting operation like
www.mysite.com/getuser/id/22
where getuser is the param name and 22 is its value.
Please provide me if there is any useful links that i can refer to.
Thanks in advance
Please read everything in the Rails Guide to Routing. There are two main cases there:
RESTful routes: only use GET, PUT, POST, DELETE. Rails maps that by using the method resources. resources :pages will lead to the following routes (and URLs) automatically:
sites GET /sites(.:format) {:action=>"index", :controller=>"sites"}
POST /sites(.:format) {:action=>"create", :controller=>"sites"}
new_site GET /sites/new(.:format) {:action=>"new", :controller=>"sites"}
edit_site GET /sites/:id/edit(.:format) {:action=>"edit", :controller=>"sites"}
site GET /sites/:id(.:format) {:action=>"show", :controller=>"sites"}
PUT /sites/:id(.:format) {:action=>"update", :controller=>"sites"}
DELETE /sites/:id(.:format) {:action=>"destroy", :controller=>"sites"}
Other routes: There is a rich set of methods you can use in your routes file to add additional routes. But keep in mind: If you just want to address a resource, it is better to stick to restful routes. A typical example is_ match ':controller(/:action(/:id))'. This allows URLs like:
localhost:3000/sites/help: controller == SitesController, action == help
localhost:3000/sites/search/something: controller == SitesController, action == search, parameter in params is something under the key id. So inside the action search, you will find params[:id] bound to "something".
In your config/routes.rb file you write:
resources :users
Then you create the controller:
rails g controller users
Inside your controller file you have something(contrived) there like :
def show
my_id_param = params[:id]
end
When you go to:
www.mysite.com/user/22
my_id_param will be 22
I think rails guides are quite good resource, just google for them, has ton of good info there.

Rails: routing question

I have this in my routes:
resources :cvits
which produces these routes:
cvits GET /cvits(.:format) {:controller=>"cvits", :action=>"index"}
POST /cvits(.:format) {:controller=>"cvits", :action=>"create"}
new_cvit GET /cvits/new(.:format) {:controller=>"cvits", :action=>"new"}
edit_cvit GET /cvits/:id/edit(.:format) {:controller=>"cvits", :action=>"edit"}
cvit GET /cvits/:id(.:format) {:controller=>"cvits", :action=>"show"}
PUT /cvits/:id(.:format) {:controller=>"cvits", :action=>"update"}
DELETE /cvits/:id(.:format) {:controller=>"cvits", :action=>"destroy"}
but I would like my urls to be singular (eg /cvit/, /cvit/new, /cvit/:id). What would be the easiest way to change this??????
Thanks!!!!
SOLVED: Figured it out, I did:
resources :cvits, :path => 'cvit'
Well:
resources :cvit
Check doc here: http://guides.rubyonrails.org/routing.html#singular-resources
Or a better fit:
resources :cvits, :path => "cvit"
Same doc page.
You just want a singular resource:
resouce :cvit
# instead of
resources :cvits
Note that your controller names etc. will still be plural (CvitsController). In order to specify otherwise you can pass:
resource :cvit, :controller => 'cvit'
Also, note that when you do this you have no index action. Singular resources assume there's only one thing there, instead of being many.
Assuming that is what you have (a singular resource), this is better than passing the path option. The path option is just overriding the name and not the behavior (i.e. you still have an index, even though that doesn't make sense if you're dealing with a singular resource).

Resources