Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 470
akaphenom Avatar asked Jul 19 '13 01:07

akaphenom


2 Answers

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.

like image 95
Shevaun Avatar answered Oct 23 '22 14:10

Shevaun


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.

like image 44
Pr. Elyou Avatar answered Oct 23 '22 14:10

Pr. Elyou