Rails Routing to New associated model

人走茶凉 提交于 2020-01-16 18:41:30

问题


I have an App that has Households that visit my agency and I am logging each time a household visits. I select the household in the households index and open the visit view which is in the visits model in my index to select a household and create a new visit I have teh following code on my index view for households:

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

Rake Routes Shows:

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

This take me to a form with the following url:

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

I Have a simple form set up at this view as so:

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

Now when I fill in the form and try to save with a submit I get:

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

Now I did notice that in Rake Routes I have a GET but not a POST but I am still not sure what my problem is?

Routes.rb shows:

  resources :households do
    resources :visits
    resources :neighbors
  end

I changed my simple form call to:

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

I am creating my form but on saving I get the error:

Required parameter missing: household

the log shows:

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)

I believe I am getting close


回答1:


Assuming you're using standard routes such as:

resources :households do
  resources :visits
end

Then you want to be posting to household_visits_path instead of new_household_visit_path.

If you run rake routes you'll probably get something similar to this:

    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

You'll notice that the only one accepting a POST request is household_visits. new_household_visit is only used to GET the form.



来源:https://stackoverflow.com/questions/17235724/rails-routing-to-new-associated-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!