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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With