【问题标题】:No route matches {:controller=>"posts", :action=>"show"}没有路线匹配 {:controller=>"posts", :action=>"show"}
【发布时间】:2013-02-25 11:07:10
【问题描述】:

当我浏览到我的索引页面时遇到路由错误。我设置了一些简单的东西,模型有: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=>"posts", :action=>"show"}

我的 routes.rb 看起来像往常一样。

routes.rb

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

任何帮助都会非常有用。

【问题讨论】:

  • 我看到的只有new@post.new应该是@post = Post.new

标签: ruby-on-rails ruby


【解决方案1】:

您的 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>

而且编辑链接也错了

【讨论】:

  • 谢谢水手,这很有效。你的意思是“编辑”字段有问题?我试过下面的
  • 您必须将您的编辑链接放入块中并调用link_to 'edit', edit_post_path(post)
【解决方案2】:

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

$ rake routes

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-18
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多