[英]How do you update a nested has_one resource?
我似乎有两个问题
1)在尝试更新单数嵌套资源时
餐厅has_one小时
和
一小时属于餐厅
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
我知道了,但这显然不是RESTful的方式。
我只需要添加:
<%= form_for hour, :url => { :action => "update" }, :html => { :method => 'put' } do |f| %>
并指定要执行更新的操作/方法。
另一篇文章建议仅使用:
<%= form_for @hour do |f| %>
本来也可以的,但是由于某种原因,我对此没有任何运气。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.