【问题标题】:How do you update a nested has_one resource?如何更新嵌套的 has_one 资源?
【发布时间】:2012-08-07 22:54:20
【问题描述】:

我好像有两个问题

1) 在尝试更新我的单一嵌套资源时

餐厅有一个小时

餐厅营业时间

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

在 //hour 之前使用双斜杠。我怀疑这会中断生产,但似乎不会影响我的开发。

再次感谢您的阅读,祝您阅读愉快!

编辑:这是 rake 路线--

  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

【问题讨论】:

  • 能否在控制台中发布 rake 路由的结果?
  • 刚刚添加了 rake 路由,感谢阅读!
  • resource :hour更改为resource :hours
  • 将资源:小时更改为资源:小时,但我在提交表单时仍然遇到相同的错误。这非常令人沮丧!但感谢您的帮助!

标签: ruby-on-rails ruby routing partials


【解决方案1】:

我想通了,但这显然不是 RESTful 方式。

我只需要补充:

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

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

另一个帖子建议只使用:

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

也可以,但由于某种原因,我没有任何运气。

【讨论】: