I'm trying to build a simple rails application however I get a routing error. Here is the controller:
class PostsController < ActionController::Base
def index
#var = "Rails is amazing"
end
end
Here is the routing:
get "/posts", to: "posts#index"
And the routing error is as following:
uninitialized constant PostsController
The url im accessing is this one:
http://localhost:3000/posts#
I understand that controllers should be pluralised in both the routing and in the name of the file. I am sorry for such a novice question
I believe, you have posts_controller.rb file in the controllers folder.In the posts_controller.rb file add the following syntax
class PostsController < ApplicationController
end
In your routes file, try adding
resources :routes
In the terminal if you will type CONTROLLER=posts rake routes, you ll get the following output
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
Related
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.
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.
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>
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.
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