2012-08-07 80 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| %> 

会工作了,但是我没有用,由于某种原因任何运气。