Can I update my nested resource without going through the parent class?
I put this in the routes
put 'ratings/:id' => 'ratings#update', :as => 'update_ratings'
and I put an exception for [:edit, :update] in the nested resource route
My controller
def update/edit
@rating = Rating.find(params[:id])
...
end
My edit view:
<%= form_for(@rating, :url => update_ratings_path(@rating)) do |f| %>
What happens is the server log says the put request happens, but no attributes are updated. The page then redirects to the show action, when it should go to parent class index page.
The log:
Started PUT “/ratings/21” for 127.0.0.1 at 2011-09-25 19:31:18 -0700
Processing by RatingsController#show as HTML
Parameters: {“utf8″=>”✓”, “authenticity_token”=>”g4TkuG1xK8W96VSKdl3ZwedrqIXmcg9CDt6y8IqaFh0=”, “rating”=>{“environ”=>”8”}, “commit”=>”Update Rating”, “id”=>”21”}
Rating Load (0.3ms) SELECT ratings.* FROM ratings WHERE ratings.id = 21 LIMIT 1
Rendered ratings/show.html.erb within layouts/application (9.8ms)
User Load (0.4ms) SELECT users.* FROM users WHERE users.id = 1 LIMIT 1
Completed 200 OK in 151ms (Views: 143.4ms | ActiveRecord: 4.8ms)
From experience I can say that using a nested resource controller for an unnested action often becomes unmanageable in the long run. So be sure it’s not easier to add a second controller. After all, a controller is just an interface to a resource. It’s not a problem to have two interfaces to the same resource.
Your log shows “Processing by RatingsController#show”, so it never reaches the #update action; instead it comes into #show.
The most probable cause I can think of is that you have a (named) route on a higher line in routes.rb to the ratings#show action, without a :via => ‘get’.