没有路线匹配{:controller =>“posts”,:action =>“show”}

时间:2013-02-25 11:07:10

标签: ruby-on-rails ruby

浏览到索引页面时出现路由错误。我设置了一些简单的东西,模型有:title (string):content(text)

index.html.erb

<h1>Listing all notes</h1>
<% @posts.each do |post| %>
  <h2><%= link_to post.title, post %></h2>
  <p><%= post.content %></p>
  <hr/>
<% end %>
<small><%= link_to "Add New Note", new_post_path %></small>
<small><%= link_to "Edit", edit_post_path(@post) %></small>

post_controller.rb

class PostsController < ApplicationController
def index
   @posts = Post.all
end

def show
   @post = Post.find(params[:id])
end
def new
   @post.new
end

def create
   @post = Post.new(params[:post])

   if @post.save
       redirect_to posts_path, :notice => "Your Note was Saved!"
   else
       render "new"
   end
end

def edit
   @post = Post.find(params[:id])
end

def update
   @post = Post.find(params[:id])

   if @post.update_attributes(params[:post])
       redirect_to posts_path
   else
       render "edit"
   end
end

当我在:3000/posts上浏览应用时,收到错误:

  

没有路线匹配{:controller =&gt;“posts”,:action =&gt;“show”}

我的routes.rb看起来很平常。

的routes.rb

NotePad::Application.routes.draw do
   resources :posts

任何帮助都非常有用。

2 个答案:

答案 0 :(得分:3)

index.html.erb文件中存在错误。正确的语法是:

<h1>Listing all notes</h1>
<% @posts.each do |post| %>
  <h2><%= link_to post.title, post_path(post) %></h2>
  <p><%= post.content %></p>
  <hr/>
<% end %>
<small><%= link_to "Add New Note", new_post_path %></small>

编辑链接也是错误的

答案 1 :(得分:3)

这不会解决您的问题,但它可以帮助您:

$ rake routes

这将显示您应用中的所有路线。