I am sorry for asking what may be a remedial question, but in learning rails i was trying to follow along note for note in this tutorial:
http://guides.rubyonrails.org/getting_started.html#configuration-gotchas
I am fie to section 5.7 - showing the results of the post, as instructed I add this line to routes.rb
post GET /posts/:id(.:format) posts#show
and the show method in posts_controller.rb:
class PostsController < ApplicationController
def new
end
def create
#post = Post.new (post_params)
#post.save
redirect_to #post
end
def show
#post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
my routes.rb file is
Listing::Application.routes.draw do
get "welcome/index"
post GET /posts/:id(.:format) posts#show
resources :posts
# You can have the root of your site routed with "root"
root 'welcome#index'
end
Here is the error:
C:/Ruby-Projects/listing/config/routes.rb:4: syntax error, unexpected
':', expecting keyword_end post GET /posts/:id(.:format) posts#show ^
Rails.root: C:/Ruby-Projects/listing
Application Trace | Framework Trace | Full Trace This error occurred
while loading the following files:
C:/Ruby-Projects/listing/config/routes.rb
I am running rails 4.0, ruby 2.0 on 64 bit windows 8.
Admittedly I don't know what that line in the routes.rb is trying to do, but my goal was to type this in and pickup what i can, before digging into the subject full bore. i cut and pasted the line, typed it in, and tried changing a couple of things - without results.
I am tired, and feeling stupid, so I am here asking for your help.
Thank you in advance.
That line in section 5.7 is just showing you the output of rake routes, it's not meant to be in your config/routes.rb file.
The line resources :posts in routes.rb generates the show posts route for you, test it by removing the line: post GET /posts/:id(.:format) posts#show and then running rake routes on the command line.
i'm new to the ruby world i have started learning it this afternoon :)
i had the same error as yours and i solved it by changing the way routes have been written to the suggested style within the routes.rb file.
instead of what have been written on that tutorials copy and past this into your routes.rb
Blog::Application.routes.draw do
get "welcome/index"
resources :posts
root 'welcome#index'
get '/posts/:id(.:format)' => 'posts#show'
get '/posts(.:format)' => 'posts#index'
end
save and check your posts url as suggested on that tutorials
http://localhost:3000/posts
it should work for you.
Related
I have defined three routes in routes.rb
Rails.application.routes.draw do
root to: 'pages#lottery'
get 'pages/about'
get 'pages/contact'
get 'pages/lottery'
end
And when I run "rake routes" in my command line, I get the following:
Prefix Verb URI Pattern Controller#Action
root GET / pages#lottery
pages_about GET /pages/about(.:format) pages#about
pages_contact GET /pages/contact(.:format) pages#contact
pages_lottery GET /pages/lottery(.:format) pages#lottery
But when I got to the localhost:3000/pages/contact I get the error:
"No route matches [GET] "/pages/contact.html.erb"
And also "You don't have any routes defined!"
Does anybody know the problem?
Ugh rookie mistake. I had two tabs open in my command line. My rails s pages was in a different directory. Thanks for the help everyone, I appreciate it.
You should not be going to the URL /pages/contact.html.erb, it should be just /pages/contact.html.
Rails provides nice helpers to make it easy to get the right path, for example, pages_contact_path (from your rake routes).
Your pages_controller.rb should consist of three methods based on your routes listed.
pages_controller.rb
class PagesController < ApplicationController
def lottery
end
def about
end
def contact
end
end
views/pages/contact.html.erb
<p>Hello World</p>
Start up your rails server - rails s and navigate to localhost:3000/pages/contact and you should see Hello World
I'm trying to follow this guide:
http://guides.rubyonrails.org/getting_started.html#hello-rails-bang
and in section 5.7 it says to add this line.
post GET /posts/:id(.:format) posts#show
What does this do, and where should I put it? I tried putting it in the "create" method that follows this form:
http://dixonc3-72812.use1.nitrousbox.com/posts/new
I also tried putting it in the "view" because I figured it was accepting the "POST" method. How do I proceed? New to Ruby on Rails and trying to figure out the kinks.
I'm trying to follow this guide:
http://guides.rubyonrails.org/getting_started.html#hello-rails-bang
and in section 5.7 it says to add this line.
post GET /posts/:id(.:format) posts#show
Section 5.7 doesn't tell you that. What it tells you is the following is output of rake routes for the show action:
post GET /posts/:id(.:format) posts#show
The output is presented in a tabular format where the four columns are (from left to right): Prefix, Verb, URI Pattern, Controller#Action.
In order to get this, you need to declare posts as a resource in config/routes.rb as:
resources :posts, only: [:show]
Now if you run rake routes in your terminal, you will see the line included from the guide.
The above line in config/routes.rb defines a route for the show action. You can remove only: [ :show ] option to have routes defined for all standard RESTful actions, i.e. in your config/routes.rb:
resources :posts
This is the routes for show action of posts controller, for create action you find something like
posts#create
This question already has answers here:
Bug in tutorial: syntax error, unexpected ':', expecting keyword_end
(2 answers)
Closed 7 years ago.
I am following the getting started tutorial here:
http://guides.rubyonrails.org/getting_started.html
I am finding routes quite confusing:
We still need a way to list all our posts, so let's do that. We'll use a specific route from config/routes.rb:
posts GET /posts(.:format) posts#index
When adding this to my routes.rb file, I encounter an error
SyntaxError
/Users/example/blog/config/routes.rb:12: syntax error, unexpected ':', expecting keyword_end #get 'posts/:format' => 'posts#index' ^ /Users/thoad/blog/config/routes.rb:12: syntax error, unexpected tIDENTIFIER, expecting keyword_end #get 'posts/:format' => 'posts#index' ^
When I just omit the line altogether, everything works fine...
So... why do I need that line, and could someone explain the structure of it to me please?
This is not a line you have to add to config/routes.rb. This line comes from running the following command: rake routes. The line means that action index in controller posts is responsible for handling the path /posts. The next step of the guide is creating said action. In your controllers and views, you can use posts_path in order to redirect to this specific path or link to it. Hope this helps.
I must say that bit of the documentation is a little misleading, but jump back a few steps in the documentation to 5 Getting up and running and you'll find the code
Blog::Application.routes.draw do
resources :posts
root to: "welcome#index"
end
This is what you should have in your routes.rb file, and it's what is responsible for producing the output that you have in your question.
If you put that into your routes.rb file, and then in a console, run rake routes you'll get the output
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 / welcome#index
Notice that in that output, the first line is what is in your question. The routing system is pretty complex in Rails, so don't get too overwhelmed now. For now, stick with the very basics.
If you want to add routes to Create, Read, Update, andDeletea record, then you want to use theresources` method in your routes file, as shown above. This will automatically create 8 routes for you. Rails will automatically associate it with the appropriate controller as well so you must make sure you name things correctly.
I'm programming for the first time in ruby and so I'm doing the 'getting started' tutorial from the official website:
http://guides.rubyonrails.org/getting_started.html
I have a problem with 5.7. The tutorial says:
If you submit the form again now, Rails will complain about not
finding the show action. That's not very useful though, so let's add
the show action before proceeding.
And then there is the following code:
post GET /posts/:id(.:format) posts#show
But where do I have to put this code?
Thanks!
What you've depicted is the show member for the posts resource routes. It's not actually code, but rather, a pattern for URL routing. You can see all your routes in this fashion by typing rake routes from the command line.
Breaking down the route:
post GET /posts/:id(.:format) posts#show
# `post` => named route name (available by default only to singular routes)
# `GET` => HTTP method
# `/posts/:id(.:format)` => path made accessible by route
# :id => specifies that the argument passed in as `:id` is available to the controller as `params[:id]`
# `posts#show` => controller is `posts`, action is `show`
You need to create a corresponding show controller action that the route will map to:
# app/controllers/posts_controller.rb
def show
#post = Post.find(params[:id])
end
I just ran into this same problem going through the tutorial. A more direct response to this question is "no where". The reference to that line int the tutorial is only informational. It reads in a way that leads you to believe that you are supposed to add it but there is nothing to add. Just keep going through the rest of the tutorial and all will be well. Maybe someday the author will read this and fix it.
Adding the following (bolded) sentence would make the instructions in 5.7 more clear:
If you submit the form again now, Rails will complain about not finding the show action. That's not very useful though, so let's add the show action before proceeding. Running 'rake routes`now results in the following:
post GET /posts/:id(.:format) posts#show
If you have the PostsController in your application, then you must have the following in the config/routes.rb
resources :posts
So that it will generate 7 default routes for the posts controller where show is a default action.
When you do rake routes in your console, it will show you all the routes for your application. From those routes, you can get
post GET /posts/:id(.:format) posts#show
The above is the routes, not the code. So it states that you have a controller named "posts" and "show" is an action of it. Which can be accessible via '/posts/:id' with "get" method.
Make sure the private section comes after the def show block
class PostsController < ApplicationController
def new
end
def create
#render text: params[:post].inspect
#post = Post.new(post_params)
#post.save
redirect_to #post
end
def show
#post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
Nothing needs to be added to the config file, as some guys already said, is the information that appears after run the command
rake routes
Just adding the show action to the post_controller.rb and the show.html.erb view is enough to be able to post the information and continue with the example.
longtime reader & first time poster, so I'd appreciate it if you went easy on me.
Recently started teaching myself RoR, and have been hacking away at a personal project/website to get the hang of things. Here's my problem:
I'm using the Simple Navigation gem to generate links. Inside navigation.rb I'm trying to call:
primary.item :home, 'Home', home_path
...where home is a view and controller that displays my front page:
home > index.html.erb (just contains a bunch of standard HTML, but let me know if it'd be useful to include)
and controllers > home_controller.rb:
class HomeController < ApplicationController
def index
#posts = Post.all
end
end
I'm getting this error, though:
Routing Error
No route matches {:action=>"show", :controller=>"home"}
Try running rake routes for more information on available routes.
... so I run rake routes, and can definitely see "home#show" in there.
My routes.rb file, as well, has this in it:
get "projects/index"
get "offer/index"
get "space/index"
get "home/index"
resources :posts
resources :home
So I'm a little baffled, and I'm sure it's because of my inexperience or general inability to understand what I'm doing, but I'd really appreciate some help as it's more or less a road block that I haven't been able to overcome.
Appreciate it!
Jay
It is because of the resources; if you are not using the resources remove resources :home
This could be your routes:
get "projects/index"
get "offer/index"
get "space/index"
get "home/index", :as => "home"
resources :posts
See how I removed the resources :home. In home/index the :as represents an alias, so you can use the alias as a method, adding "path" at the end of the name.
Check this guide about routes and resources: http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default