Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when does rails looks for Index method and when for show method in controller

I'm new to Rails and I have been following a tutorial.

I have been fiddling around with routes.rb and now in total confusion about when it looks for show method and when for index method if not explicitly mentioned?

like image 766
dotNETbeginner Avatar asked Sep 08 '17 10:09

dotNETbeginner


People also ask

What decides which controller receives which requests Ruby on Rails?

Routing decides which controller receives which requests. Often, there is more than one route to each controller, and different routes can be served by different actions.

What does controller do in Rails?

The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services. It is responsible for routing external requests to internal actions.

What is the difference between resource and resources in Rails?

Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.

What is Before_filter in Ruby on Rails?

Before Filters ADVERTISEMENT. ADVERTISEMENT. Rails before filters are executed before the code in action controller is executed. The before filters are defined at the top of a controller class that calls them. To set it up, you need to call before_filter method.


2 Answers

Routes are simply like regexes on steroids. They have priority in the order they are defined and match the request method, URI and any other constraints that you have added.

 get '/posts', to: 'posts#index'
 get '/posts/:id', to: 'posts#show'

The key difference to the routes above is that the regular expression which the path of request uri must match is different. '/posts/:id' includes a named segment which would match:

GET /posts/11
GET /posts/gobligook

But not:

GET /posts
GET /posts/1/foo
GET /posts/foo/bar

The full conventional set of CRUD verbs are:

get    '/posts',          to: 'posts#index'   # all posts
get    '/posts/new',      to: 'posts#new'     # form to create a post
post   '/posts',          to: 'posts#create'  # create a post from a form submission
get    '/posts/:id',      to: 'posts#show'    # show a single post
get    '/posts/:id/edit', to: 'posts#edit'    # form to edit a post
put    '/posts/:id',      to: 'posts#update'  # for legacy compatibility 
patch  '/posts/:id',      to: 'posts#update'  # update a post from a form submission
delete '/posts/:id',      to: 'posts#destroy' # delete a post

In Rails flavor REST the action is implicitly derived from the path and method.
Only the new and edit methods which are used to render forms actually explicitly add the action in the path - which is because they act on a collection or a member of a collection.

Note that the '/posts/new' route must be declared before get '/posts/:id' or the show route would match the request first (routes have priority in the order they are defined). This does not apply to get '/posts/:id/edit' since the pattern is different.

Of course typing out all those routes is really tedious so rails provides the resources macro that does this for you:

resources :posts
like image 141
max Avatar answered Sep 20 '22 18:09

max


Both index and show are GET method, but the difference is index is collection type and show is member type. Which means index does not expect any parameter in the url, but show expects id param in the url.

EX:
Index: /posts
Show: /posts/:id
like image 43
Mohanraj Avatar answered Sep 21 '22 18:09

Mohanraj