Rails尝试演出的新动作

时间:2012-06-16 04:32:48

标签: ruby-on-rails ruby-on-rails-3 routes

我正在开始一个新的Rails 3.2.6应用程序。当我尝试查看新的嵌套表单时,我的嵌套路由失败。我将首先显示错误,然后显示所有涉及的代码。

尝试访问的网址:http://localhost:3000/reports/1/expenses/new

No route matches {:action=>"show", :controller=>"expenses", :report_id=>#<Report id: 1, name: "Test Report", created_at: "2012-06-07 21:58:37", updated_at: "2012-06-07 21:58:37">}

的routes.rb

resources :reports do
  resources :expenses
end

expenses_controller.rb

def new
  @report = Report.find(params[:report_id])
  @expense = @report.expenses.new
end

视图/费用/ new.html.haml

%h1 New Expense
= render 'form'

视图/费用/ _form.html.haml

= form_for [@report, @expense] do |f|

这是我试图点击的链接:

= link_to 'New Expense', new_report_expense_path(@report)

当我明确调用新动作时,我无法弄清楚为什么它会尝试访问show动作。

Rake Routes

report_expenses     GET    /reports/:report_id/expenses(.:format)          expenses#index
                    POST   /reports/:report_id/expenses(.:format)          expenses#create
new_report_expense  GET    /reports/:report_id/expenses/new(.:format)      expenses#new
edit_report_expense GET    /reports/:report_id/expenses/:id/edit(.:format) expenses#edit
report_expense      GET    /reports/:report_id/expenses/:id(.:format)      expenses#show
                    PUT    /reports/:report_id/expenses/:id(.:format)      expenses#update
                    DELETE /reports/:report_id/expenses/:id(.:format)      expenses#destroy
        reports     GET    /reports(.:format)                              reports#index
                    POST   /reports(.:format)                              reports#create
     new_report     GET    /reports/new(.:format)                          reports#new
    edit_report     GET    /reports/:id/edit(.:format)                     reports#edit
         report     GET    /reports/:id(.:format)                          reports#show
                    PUT    /reports/:id(.:format)                          reports#update
                    DELETE /reports/:id(.:format)                          reports#destroy
           root            /                                               reports#index

更新 链接到GitHub回购:https://github.com/ardavis/expense_report

3 个答案:

答案 0 :(得分:4)

错误位于views / expences / _form.html.haml,最后一行

      = link_to 'Cancel', report_expense_path(@report), class: 'btn'

你可能意味着

      = link_to 'Cancel', report_path(@report), class: 'btn'

答案 1 :(得分:0)

您是否尝试过路线:

map.resources :reports do |report|
    report.resources :expenses
end

答案 2 :(得分:-1)

您需要在链接中放置@ report.id而不是@report

link_to 'New Expense', new_report_expense_path(@report.id)