2012-08-07 146 views
1

我似乎有兩個問題如何更新嵌套的has_one資源?

1)雖然試圖更新我的奇異嵌套資源

餐廳HAS_ONE小時

小時belongs_to的餐廳

resources :restaurants do 
    resource :hour 
end 

與編輯我的餐廳展示頁面上的鏈接:

<%= link_to 'Set Hour', edit_restaurant_hour_path([@restaurant, @restaurant.hour]) %> 

和編輯頁面有一個局部渲染,看起來像:

<%= render :partial => 'restaurants/hours', :locals => { :hour => 'hour' } %> 

它加載一個名爲局部_hours.html.erb:

<%= form_for hour do |f| %> 
<div class="row-fluid"> 
<div class="span1 hours_input"> 
    <h3>Monday</h1> 
    From 
    <%= f.text_field :from_monday, :class => 'span20 hour_field' %> 
    To 
    <%= f.text_field :to_monday, :class => 'span20 hour_field' %> 
</div> 
<div class="span1 hours_input"> 
    <h3>Tuesday</h3> 
    From 
    <%= f.text_field :from_tuesday, :class => 'span20 hour_field' %> 
    To 
    <%= f.text_field :to_tuesday, :class => 'span20 hour_field' %> 
</div> 
<div class="span1"> 
    <%= f.submit 'Set Hours' %> 
</div> 
</div> 

但一旦我按下提交按鈕它給了我錯誤:

No route matches [POST] "/restaurants/34/hour/edit" 

我試着把它設置爲:

<%= form_for hour, :method => put, :html => { :action => 'update' } do |f| %> 

但沒有運氣。 任何幫助將不勝感激! 我使用rails 3.2.3

2)我的第二個問題很神祕。 //小時前

http://localhost:3000/restaurants/34//hour/edit 

與雙斜線:

一旦我按在餐廳秀頁面上的按鈕

<%= link_to 'Set Hour', edit_restaurant_hour_path([@restaurant, @restaurant.hour]) %> 

,它會給網址。我懷疑這會打破生產,但似乎並沒有影響我的發展。

再一次,感謝閱讀,並有一個很好的!

編輯:這裏是routes--

restaurant_hour POST /restaurants/:restaurant_id/hour(.:format)  hours#create 

new_restaurant_hour GET /restaurants/:restaurant_id/hour/new(.:format)  hours#new 

    edit_restaurant_hour GET /restaurants/:restaurant_id/hour/edit(.:format) hours#edit 

    GET /restaurants/:restaurant_id/hour(.:format)    hours#show 

    PUT /restaurants/:restaurant_id/hour(.:format)    hours#update 

    DELETE /restaurants/:restaurant_id/hour(.:format)    hours#destroy 

    restaurants GET /restaurants(.:format)      restaurants#index 

    POST /restaurants(.:format)         restaurants#create 

    new_restaurant GET /restaurants/new(.:format)    restaurants#new 

    edit_restaurant GET /restaurants/:id/edit(.:format)   restaurants#edit 

    restaurant GET /restaurants/:id(.:format)     restaurants#show 

    PUT /restaurants/:id(.:format)        restaurants#update 

    DELETE /restaurants/:id(.:format)        restaurants#destroy 

    hour GET /:restaurant/hour(.:format)       hours#show 

    POST /:restaurant/hour(.:format)        hours#create 

    hour_add GET /:restaurant/hour/add(.:format)     hours#new 

    hour_edit GET /:restaurant/hour/edit(.:format)       hours#edit 
+0

你可以發佈在控制檯做耙路線的結果嗎? – Nobita 2012-08-08 04:37:22

+0

剛剛添加了耙路線,謝謝閱讀! – 2012-08-08 05:14:31

+0

將'resource:hour'更改爲'resource:hours' – 2012-08-08 07:52:24

回答

0

我想通了耙,但它顯然沒有做到這一點的RESTful方式。

我不得不補充:

<%= form_for hour, :url => { :action => "update" }, :html => { :method => 'put' } do |f| %> 

,並指定更新工作的操作/方法。

另一篇文章認爲,只是用:

<%= form_for @hour do |f| %> 

會工作了,但是我沒有用,由於某種原因任何運氣。