如何更新嵌套的has_one资源?

时间:2012-08-07 22:54:20

标签: ruby-on-rails ruby routing partials

我似乎有两个问题

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)我的第二个问题相当神秘。

按下按钮后

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

在餐厅展示页面上,它会给出网址:

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

在//小时之前使用双斜杠。我怀疑这会破坏生产,但似乎不会影响我的发展。

再次感谢阅读并有一个好的!

编辑:这是佣金路线 -

  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

1 个答案:

答案 0 :(得分:0)

我明白了,但显然不是RESTful方式。

我只需要添加:

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

并指定更新的操作/方法。

另一篇帖子建议只使用:

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

也会起作用,但出于某种原因,我没有任何运气。