I seem to have two problems
1) While trying to update my singular nested resource
Restaurant has_one Hour
and
Hour belongs_to Restaurant
resources :restaurants do
resource :hour
end
with an edit link on my restaurant show page called:
<%= link_to 'Set Hour', edit_restaurant_hour_path([@restaurant, @restaurant.hour]) %>
and the edit page has a partial render that looks like:
<%= render :partial => 'restaurants/hours', :locals => { :hour => 'hour' } %>
which loads a partial named _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>
but once I press the submit button it gives me the error:
No route matches [POST] "/restaurants/34/hour/edit"
I tried setting it as:
<%= form_for hour, :method => put, :html => { :action => 'update' } do |f| %>
but with no luck.
Any help would be greatly appreciated!
I’m using rails 3.2.3
2) My second problem is rather mysterious.
Once I press on the button
<%= link_to 'Set Hour', edit_restaurant_hour_path([@restaurant, @restaurant.hour]) %>
on the restaurant show page, it’ll give the url:
http://localhost:3000/restaurants/34//hour/edit
with the double slash before //hour. I suspect this will break in production but doesn’t seem to affect me in development.
Again, thanks for reading and have a good one!
Edit: Here’s the rake 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
I figured it out, but it’s apparently not the RESTful way to do it.
I just had to add:
and specify the action/method for the update to work.
Another post suggested that just using:
would have worked, too, but I wasn’t having any luck with that for some reason.