1

インデックス ページを参照すると、ルーティング エラーが発生します。モデルには: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 はいつものように見えます。

ルート.rb

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

どんな助けでも非常に役に立ちます。

4

2 に答える 2

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>

そして、編集リンクも間違っています

于 2013-02-25T11:11:33.003 に答える
3

これはあなたの問題を解決しませんが、あなたを助けることができます:

$ rake routes

これにより、アプリにすべてのルートが表示されます。

于 2013-02-25T11:12:12.233 に答える