小编Bar*_*ian的帖子

如何在rails中添加单个自定义路由?

我有一个'事务'模型,控制器和视图,我用rails generate创建.现在我需要向我的应用程序添加一个/ transactions/history的自定义路由,由控制器def历史记录处理:... end和render history.html.erb

所以在我的routes.rb中添加了这一行:

get '/transactions/history', to: 'transactions#history', as: 'transactions_history'
Run Code Online (Sandbox Code Playgroud)

这在我的transactions_controller.rb中:

def history
    @transactions = Transaction.all
end
Run Code Online (Sandbox Code Playgroud)

并在transactions-> views中创建了history.htmk.erb

调用rake路由时我也看到这一行:

transactions_history GET    /transactions/history(.:format)                 transactions#history
Run Code Online (Sandbox Code Playgroud)

但是当我在浏览器中请求localhost:3000/transactions/history时,它会给我以下错误:

Couldn't find Transaction with 'id'=history
Run Code Online (Sandbox Code Playgroud)

(因为我的控制器中有这一行)

before_action :set_transaction, only: [:show, :edit, :update, :destroy])
Run Code Online (Sandbox Code Playgroud)

我也在日志中看到这一行:

Request info

Request parameters  
{"controller"=>"transactions", "action"=>"show", "id"=>"history"}
Run Code Online (Sandbox Code Playgroud)

我的完整路线: routes.rb 我的完整错误: 错误日志 为什么它在我的事务控制器中调用'show'操作?

ruby model-view-controller ruby-on-rails ruby-on-rails-3

5
推荐指数
1
解决办法
3365
查看次数