Rails指南第5.12节中的UrlGenerationError

我正在按照Rails 4.0.0的入门教程进行操作: http : //guides.rubyonrails.org/getting_started.html#updating-posts

当我尝试从主页面(localhost)导航到localhost / posts时,我收到此错误:

ActionController::UrlGenerationError in Posts#index Showing C:/RailsInstaller/blog/app/views/posts/index.html.erb where line #16 raised: No route matches {:action=>"show", :controller=>"posts"} missing required keys: [:id] Extracted source (around line #16): 13  14  15  16  17  18  19  

此外,当我尝试编辑post时,我在edit.html.erb文件中收到一个SyntaxError:

 C:/RailsInstaller/blog/app/views/posts/edit.html.erb:3: syntax error, unexpected '}', expecting keyword_end ';@output_buffer.append= form_for :post, url: post_path(@post.id) }, ^ C:/RailsInstaller/blog/app/views/posts/edit.html.erb:32: syntax error, unexpected keyword_ensure, expecting $end 

具体来说,用这一行:

  

这是我第一次尝试学习Rails,所以当我遇到这些错误时,我很难理解在哪里开始看。 以下是一些相关文件:

posts_controller.rb:

class PostsController <ApplicationController

 def index @posts = Post.all end def new @post = Post.new end def create @post = Post.new(params[:post].permit(:title, :text)) if @post.save redirect_to @post else render 'new' end end def show @post = Post.find(params[:id]) end def update @post = Post.find(params[:id]) if @post.update(params[:post].permit(:title, :text)) redirect_to @post else render 'edit' end end def edit @post = Post.find(params[:id]) end private def post_params params.require(:post).permit(:title, :text) end end 

edit.html.rb:

 

Editing post

prohibited this post from being saved:



路线:RB:

 Blog::Application.routes.draw do resources :posts root to: "welcome#index" end 

它说错误是由这条线引起的

 <%= link_to 'Show', post_path %> 

它应该是

 <%= link_to 'Show', post %> 

要么

 <%= link_to 'Show', post_path(post) %>