0

我有一个应用程序,其中有家庭访问我的代理机构,并且每次家庭访问时我都会记录。我在家庭索引中选择家庭并打开索引中访问模型中的访问视图以选择一个家庭并创建一个新访问我的家庭索引视图中有以下代码:

 = link_to  'Visit', new_household_visit_path(household), class: 'btn btn-mini'

耙路线显示:

new_household_visit GET    /households/:household_id/visits/new(.:format)    visits#new

这会将我带到一个带有以下 url 的表单:

  http://localhost:3000/households/5/visits/new

我在此视图中设置了一个简单的表单,如下所示:

= simple_form_for [household, visit], url: new_household_visit_path  do |f|
%h2 Household: #{household.name}

现在,当我填写表格并尝试通过提交保存时,我得到:

 No route matches [POST] "/households/5/visits/new"

现在我确实注意到在 Rake Routes 中我有一个 GET 但没有一个 POST 但我仍然不确定我的问题是什么?

Routes.rb 显示:

  resources :households do
    resources :visits
    resources :neighbors
  end

我将简单的表单调用更改为:

= simple_form_for [household, visit], url: household_visits_path do |f|

我正在创建我的表单,但在保存时出现错误:

Required parameter missing: household

日志显示:

Started POST "/households/8/visits" for 127.0.0.1 at 2013-06-22 07:58:34 -0400
Processing by VisitsController#create as HTML
  Parameters: {"utf8"=>"✓",    "authenticity_token"=>"HCivpI4SLQTzj2VlwibssNEw/o69jIt4J54/hjVPgJ4=", "visit"=>{"visited_on(1i)"=>"2013", "visited_on(3i)"=>"22", "visited_on(2i)"=>"6", "neighbor_id"=>"46", "starch"=>"Beans", "cereal"=>"", "option1"=>"", "items_received"=>"", "notes"=>""}, "commit"=>"Create Visit", "household_id"=>"8"}
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
      (0.2ms)  BEGIN
  Neighbor Load (0.8ms)  SELECT "neighbors".* FROM "neighbors" WHERE "neighbors"."id"  = 46 ORDER BY last_name ASC LIMIT 1
  (0.2ms)  ROLLBACK
  Rendered visits/_new.html.haml (129.7ms)
  Rendered visits/new.html.haml within layouts/application (136.2ms)
  Rendered text template (0.0ms) 
Completed 400 Bad Request in 1126ms (Views: 7.7ms | ActiveRecord: 0.0ms)

我相信我正在接近

4

1 回答 1

0

假设您正在使用标准路线,例如:

resources :households do
  resources :visits
end

然后你想发布到household_visits_path而不是new_household_visit_path.

如果你运行rake routes,你可能会得到类似的东西:

    household_visits GET    /households/:household_id/visits(.:format)          visits#index
                     POST   /households/:household_id/visits(.:format)          visits#create
 new_household_visit GET    /households/:household_id/visits/new(.:format)      visits#new
edit_household_visit GET    /households/:household_id/visits/:id/edit(.:format) visits#edit
     household_visit GET    /households/:household_id/visits/:id(.:format)      visits#show
                     PUT    /households/:household_id/visits/:id(.:format)      visits#update
                     DELETE /households/:household_id/visits/:id(.:format)      visits#destroy

您会注意到唯一接受 POST 请求的是household_visits. new_household_visit仅用于获取表单。

于 2013-06-21T13:10:08.257 回答