Ruby on Rails get route - ruby-on-rails

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.

Related

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'

Routing in ruby for controllers

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

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>

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

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.

Resources