1
votes

I have created one controller and added default 7 action methods , I have also created 4 views and those are index , new , show , edit all contains difeent data

also added resources :posts controller configuration into my routing file as well 
'
My problem is when i try to navigate to index or edit views using url .. it displays me only show views , however index method is working as a default 

below are my urls `enter code here`

1 . http://localhost:3000/posts/index[^]

if i try this it gives me show view data

  1. localhost:3000/posts

this gives me proper index data (default without mentioning index action name)

  1. http://localhost:3000/posts/edit[^]

this one but always shows me data present in show view and not in data present in edit view

Please suggest

Below are few lines from my route file.

Hide   Copy Code
Rails.application.routes.draw do
  get 'home/index'
  resources :posts

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  root 'home#index'
2

2 Answers

0
votes

Please read the Rails guide entry on default resource routing, there's nothing unusual going on here.

/posts is how you access your posts#index, not /posts/index.

For show and edit, you need your post id in the URL so that Rails knows which record to retrieve, ie /posts/1/edit

0
votes

You can run rake routes from the console to see all of your routes. The result of resources :posts should be the following seven routes:

    Prefix Verb   URI Pattern               Controller#Action
           POST   /posts(.:format)          posts#create
 new_sheet GET    /posts/new(.:format)      posts#new
edit_sheet GET    /posts/:id/edit(.:format) posts#edit
     sheet GET    /posts/:id(.:format)      posts#show
           PATCH  /posts/:id(.:format)      posts#update
           PUT    /posts/:id(.:format)      posts#update
           DELETE /posts/:id(.:format)      posts#destroy

Rails will match the URL to the URI pattern and take the matching controller action. btw you can use rake routes CONTROLLER=posts to see just the routes for posts.