我有一个'事务'模型,控制器和视图,我用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)