Rails “Getting Started” routes: can't get the "posts#index" - ruby-on-rails

I am following the getting started tutorial here:
http://guides.rubyonrails.org/getting_started.html
My route.rb
Blog::Application.routes.draw do
resources :posts
root to: "welcome#index"
end
With the command"rake route", I get :
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root / welcome#index
I can't find one route " posts GET /posts(.:format) posts#index"
My rails version is 4.0.2. Could someone give me some help please?
I added another resource as following:
Blog::Application.routes.draw do
# get "welcome/index"
resource :posts
resource :apples
root 'welcome#index'
end
Rake routes, the output is:
lidong#lidong-VirtualBox:~/blog$ rake routes
Prefix Verb URI Pattern Controller#Action
posts POST /posts(.:format) posts#create
new_posts GET /posts/new(.:format) posts#new
edit_posts GET /posts/edit(.:format) posts#edit
GET /posts(.:format) posts#show
PATCH /posts(.:format) posts#update
PUT /posts(.:format) posts#update
DELETE /posts(.:format) posts#destroy
apples POST /apples(.:format) apples#create
new_apples GET /apples/new(.:format) apples#new
edit_apples GET /apples/edit(.:format) apples#edit
GET /apples(.:format) apples#show
PATCH /apples(.:format) apples#update
PUT /apples(.:format) apples#update
DELETE /apples(.:format) apples#destroy
root GET / welcome#index
I also can't get the 'apples#index'.

It's because in your routes you're declaring a singular resource:
resource :posts
You want to do istead:
resources :posts
Refer to the documentation for singular resources & plural resources for more info.

Related

Ruby on Rails get route

I'm totally new on Ruby, I need to create route get which will write dynamically text = "About Us"
about.html.erb code:
<h1><%= #title %></h1>
index.html.erb code:
<h1>Index</h1>
Routes code:
Rails.application.routes.draw do
root 'posts#index'
get 'about' => 'pages#about'
end
In the folder controllers I have defined pages_controller.rb and posts_controller.rb,pages with simple function about and posts with function index
In the folder views/pages I have about.html.erb and views/posts index.html.erb
The problem is when I run it, it writes message only from index and not from about.html.erb
Rake routes message:
$ rake routes
Prefix Verb URI Pattern Controller#Action
root GET / posts#index
about GET /about(.:format) pages#about
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
Since you want to call the about action and you named it about in your routes.rb file, you have to point your browser to:
localhost:3000/about
You also have to set your #title variable to something in the pages#about action.

Why I need 'show.html.erb' template for going to '.../posts/index'?

I am trying to make my first Ror app and here is my first problem :)
When I go to localhost:3000/posts/index, I get message 'Missing template posts/show, application/show with {:locale...' Why is that? Why I need show.html.erb template for posts/index?
routes.rb file
Rails.application.routes.draw do
resources :posts
root 'posts#index'
end
Routes after $rake routes command:
Prefix Verb URI Pattern Controller#Action
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root GET / posts#index
If you inspect your routes you'l will notice that the posts#index action is mapped to /posts, not posts/index.
What's happening here is that /posts/index is getting mappend to /posts/:id, with index being set as the id. With a GET request this gets mapped to the show action.

How do I create a new route path to my newly generated action?

I have created a new action in Posts controller, However, it seems that I can not access it.
the action name is 'kill' , so when I type in my browser, localhost:3000/posts/kill
I get the following error : Couldn't find Pad with id=kill
Extracted source (around line #18
def show
#post_selected = Post.find(params[:id])
#posts = Post.all
end
In my routes.rb
resources :posts
match ':controller(:action(/:id))', :via => [:get, :post]
match "/pads/modifier" => "pads#modifier", :via => [:get]
and when I type rake routes , this is what i get
Prefix Verb URI Pattern Controller#Ac
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root GET / posts#welcome
GET|POST /:controller(:action(/:id))(.:format) :controller#:
posts_modifier GET /posts/modifier(.:format) posts#modifier
How do I generate a path to get my newly created action 'kill'?
=========================================================
Update #1 : Solved,
In addition to the answer below, I could make it happen with the following code
resources :posts, controller: 'posts' do
get 'posts/:action', to: 'posts#:action'
end
===========================================================
get 'posts/kill', to: 'posts#kill'

routes REST redirection is not working properly in Rails 4.0

I have added new resources to my application.
resources :posts do
resources :comments
end
Rake routes for the same is also showing correct redirection paths.
rake routes CONTROLLER=posts
Prefix Verb URI Pattern Controller#Action
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
But when i use following lines of code :
<%= link_to 'Get Complete Blog List', posts_path %>
t's throwing error :
undefined method `posts_path' for #<#<Class:0xb464058c>:0xb4e8a274>

Routing error in tutorial app converted from rails 2.x to 3.x

I'am working on sixrevision.com tutorial “How to Create a Blog from Scratch Using Ruby on Rails” . I try to convert It from rails 2.x to 3.x. When I run localhost:3000 I get this:
Routing Error
uninitialized constant PostController
Try running rake routes for more information on available routes.
Rake Routes shows me this:
posts GET /posts(.:format) posts#index {:has_many=>:comments}
POST /posts(.:format) posts#create {:has_many=>:comments}
new_post GET /posts/new(.:format) posts#new {:has_many=>:comments}
edit_post GET /posts/:id/edit(.:format) posts#edit {:has_many=>:comments}
post GET /posts/:id(.:format) posts#show {:has_many=>:comments}
PUT /posts/:id(.:format) posts#update {:has_many=>:comments}
DELETE /posts/:id(.:format) posts#destroy {:has_many=>:comments}
/:controller/:action/:id(.:format) :controller#:action
/:controller/:action/:id.:format :controller#:action
root / post#index
My routes.rb file:
Myblog::Application.routes.draw do
resources :posts, :has_many => :comments
match ':controller/:action/:id'
match ':controller/:action/:id.:format'
root :to => "post#index"
end
Anybody got any idea? Thanks for intrest & help!
In routes.rb file:
resources :posts do
resources :comments
end
In your routes.rb you should have:
root :to => 'posts#index'
because you do not have (probably) PostController but PostsController

Resources