SyntaxError attempting to complete Getting Started with Rails - ruby-on-rails

I'm new to Linux/Ruby/Rails, so I'm trying to learn by doing the Getting Started with Rails tutorial. In section 5.7, it doesn't specifically say what to do with this line:
post GET /posts/:id(.:format) posts#show
I assume I am to put this in the routes.rb file? I did, but then I get this when I try to GET any of the controller actions:
SyntaxError
/.../blog/config/routes.rb:9: syntax error, unexpected ':', expecting keyword_end post GET /posts/:id(.:format) posts#show ^
Being such a newbie, I have no idea what I should do at this point. What is the error on this line?
Thanks,
James

You do not put the following in the config/routes.rb file.
post GET /posts/:id(.:format) posts#show
This is the result of a route entry, which is what you'd put in your routes.rb file. E.g.
get 'posts/:id', to: "posts#show"
Here get is the HTTP method, posts/:id is the path pattern, and the to: "posts#show" is the name of the controller and action. So, when this pattern is encountered, Rails is going to execute show action in PostsController.
Recommend a read on "Rails Routing from the Outside In".

Related

Ruby on rails guide

For the guide that shows you how to set up a blog, I've gotten up to 5.7.
For this section it tells you how to show the article, but after following the steps I only get an error when attempting to run rails server.
It exactly says it is a syntax error in a routes line.
The line is "article get /articles/:id(.:format) articles#show" and the error is pointing to :id.
I'm not really sure what to do, I've looked at the routes section of the guide and I thought it was a valid line.
routes.rb contains
Rails.application.routes.draw do
get 'welcome/index'
resources :articles
root 'welcome#index'
article get /articles/:id(.:format) articles#show
end
The exact message I receive when I use the command rails server is:
blog/config/routes.rb:5: syntax error, unexpected ':', expecting keyword_end (SyntaxError) article get /articles/:id(.:format) article#show
The route you're trying to make is bad formatted, I could say you literally pasted the output from the rails routes command.
You need to specify firstly the HTTP verb, GET in this case, then the url which this will respond with, and then the controller and action.
From:
article get /articles/:id(.:format) articles#show
Try with:
get '/articles/:id', to: 'articles#show'
Based on what you've put in your description, it looks like you're at the very least missing a hash rocket (=>) in one of your routes. However, as you've added the resources :articles line, I think the line that's produced this error could probably be removed altogether as the resources keyword will generate common routes like these automatically (please see the Rails API for more details).
Your routes.rb file should probably just look like this:
Rails.application.routes.draw do
get 'welcome/index'
resources :articles
root 'welcome#index'
end
Alternatively, the route you're having trouble with could be rewritten as:
get "/articles/:id" => "articles#show"
or in the way described in the guide:
get '/articles/:id', to: 'articles#show'
If you remove this line:
article get /articles/:id(.:format) articles#show
it should get ride of the error.
This line is not formatted correctly and it's trying to accomplish what is already being accomplished with the resources :articles line.

Ruby on Rails using a POST method

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

No route matches [GET] "/posts/new"

I am completely new to ruby, andI am following this ruby rails tutorial step by step, except for the detail that I've called my app "cinema".
I created a resource named "posts", there is a controller class called posts controller. From the page posts/new I should post a text with title, and execute an action (show). I am trying to add the show action in the routes file and in the controller class.
The show action should be called when a form is submitted, the form includes a title and a text field, mapped into the database.
In paragraph 5.7, there is a non-clear instruction: it says to add this line:
post GET /posts/:id(.:format) posts#show
To my routes.rb file, but it doesn't say where to write it exactly, I put it under resources:posts (maybe it's the wrong place, it doesn't saying anything about that).
I also added the show method to the controller class:
def show
#post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title,:test);
end
But when I submit the form I still get this error:
The rake routes command result:
Prefix Verb URI Pattern Controller#Action
welcome_index GET /welcome/index(.:format) welcome#index
root GET / welcome#index
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
It doesn't tell you to add it to routes.rb. It's one of the routes that is created automatically with:
resources :posts
Remove the line from your routes.rb restart the server and continue with the tutorial.
Tip: you can run rake routes to see all available routes in your application.
Your whole question seems completely contradictory to me.
You never do this in rails routes -
post GET /posts/:id(.:format) posts#show
Instead in routes.rb file.
get 'posts/:id' => 'posts#show'
The routing is done on a priority basis (first come first serve), so if your are adding get 'posts/:id' after the resources :posts, it is of no use as resources :posts already does it for you (read the rails routing guide on resources).
Your form submission should be a post data, if you are using resources 'new' should be a 'get' data and the corresponding 'post' should be 'create'. Your 'new' route has an error, then where else are you rendering your form to submit the form data?
My suggestion is that you keep the 'resources :post' and remove every other this corresponding to your :post from routes file. If you have everything else right then it should probably work fine.
resources :posts
delete it
rails s
paste resources :posts
rails s
I make it and success

Rails "Getting Started" routes [duplicate]

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.

Bug in tutorial: syntax error, unexpected ':', expecting keyword_end

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.

Resources