2014-02-20 80 views
0

我在軌道上的紅寶石的第一個項目。我得到這個錯誤enter image description hereActiveRecord :: RecordNotFound

我print.html.erb是一個靜態page.It有一個鏈接<a href="posts/index">Show</a>

和打印頁面在我的情況下,索引頁即本地主機:3000打開打印頁面。

這是我index.html.erb頁面(這是鏈接的頁面)

<h1>Listing posts</h1> 

<table> 
    <tr> 
    <th>Title</th> 
    <th>Text</th> 
    </tr> 

    <% @posts.each do |post| %> 
    <tr> 
     <td><%= post.title %></td> 
     <td><%= post.text %></td> 
    </tr> 
    <% end %> 
</table> 

這是我的控制器

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

def new 
    end 

def create 
    @post = Post.new(post_params) 
    @post.save 
    redirect_to @post 
end 

    def post_params 
    params.require(:post).permit(:title, :text) 
    end 

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

def print 
end 

end 

這是我的路線文件

Watermark::Application.routes.draw do 
resources :posts 
    root "posts#print" 

    post 'posts/index' => 'posts#index' 
    post ':controller(/:action(/:id(.:format)))' 
    get ':controller(/:action(/:id(.:format)))' 
end 

我想問題是在路線文件。

+0

HTTP動詞類似'get','post'聲明應該放在'resouce:post'和'resouce:post'後面,以處理所有'CURD'操作,不需要再次聲明它 –

回答

1

你的路由包含一些虛假的補充。您不應該添加

post 'posts/index' => 'posts#index' 

這隻會與現有路線衝突。你應該刪除它。

resources :posts是所有你需要生成seven default RESTful routes in Rails,包括index,它只是通過/posts,不應該/posts/index

你也應該刪除這兩個包羅萬象的路線,他們沒有用了。看起來你可能從一篇相當過時的教程開始工作。

+0

它可以工作......你能告訴我把'/ posts/index'爲什麼會顯示動作? –

+0

'刪除兩條全路徑'??? –

+0

因爲您正在使用'resources:posts',它定義了一個'GET/posts /:id'路由,路由文件比您的自定義路由更高。您在發佈GET請求時在地址欄中輸入了「posts/index」。它匹配'/ posts /:id',id爲「index」。如果您正在發佈POST請求,它會匹配您的自定義'發佈'帖子/索引'路線。 – meagar

相關問題