2014-02-20 42 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

相关问题