很抱歉问到什么是补救性问题,但是在学习Rails时,我试图按照本教程中的注释进行操作:
http://guides.rubyonrails.org/getting_started.html#configuration-gotchas
我喜欢第5.7节-显示帖子的结果,按照指示,我将此行添加到route.rb
post GET /posts/:id(.:format) posts#show
和posts_controller.rb中的show方法:
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
我的routes.rb文件是
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
这是错误:
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
我在64位Windows 8上运行rails 4.0,ruby 2.0。
诚然,我不知道routes.rb中的那行试图做什么,但是我的目标是在深入研究主题之前将其键入并拾取我所能做的。我剪切并粘贴了线条,输入了内容,然后尝试更改了几件事-没有结果。
我很累,感觉很愚蠢,所以我在这里寻求您的帮助。
先感谢您。
最佳答案
5.7节中的那一行只是向您显示rake routes
的输出,而不是放在config/routes.rb文件中。
routes.rb中的resources :posts
行会为您生成show posts路由,请删除以下行对其进行测试:post GET /posts/:id(.:format) posts#show
,然后在命令行上运行rake routes
。
关于ruby-on-rails - 教程: syntax error,中的错误意外的 ':',期望keyword_end,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17736811/