1

我似乎有两个问题

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

餐厅 has_one 小时

Hour 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| %>

但没有运气。任何帮助将不胜感激!我正在使用导轨 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
4

1 回答 1

0

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

我只需要补充:

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

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

另一篇文章建议只使用:

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

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

于 2012-08-13T00:04:17.440 回答