Reputation: 1
I am experiencing a strange behavior for a certain record in my routes.rb. Here is an excerpt of my routes.rb:
resources :stores do
resources :stores_spare_parts do
collection do
post :update_attribute_on_the_spot
end
end
end
When I now call the method url_for
in my index action of the stores_spare_parts Controller, it returns http://127.0.0.1:3000/stores/1/stores_spare_parts/update_attribute_on_the_spot
. But when I do the same in my create action it stops working with the following error:
ActionController::RoutingError
(No route matches {:action=>"update_attribute_on_the_spot",
:controller=>"stores_spare_parts"}):
app/controllers/stores_spare_parts_controller.rb:19:in `block in create'
app/controllers/stores_spare_parts_controller.rb:19:in `create'
Here is the controller code for the index and the create method:
def index
Rails.logger.debug { "---------------------------" }
Rails.logger.debug { url_for(:action => 'update_attribute_on_the_spot') }
Rails.logger.debug { "---------------------------" }
@currentStore = Store.find(params[:store_id])
@activeStores = Store.scoped_by_deactivated(false)
@storesSpareParts = StoresSparePart.scoped_by_store_id(@currentStore.id)
end
def create
Rails.logger.debug { "---------------------------" }
Rails.logger.debug { url_for(:action => 'update_attribute_on_the_spot') }
Rails.logger.debug { "---------------------------" }
return
@newStoreSparePart = StoresSparePart.new(params[:stores_spare_part])
if @newStoreSparePart.save
flash[:notice] = "Successfully updated spare part for store #{@newStoreSparePart.store.title}."
@currentStore = @newStoreSparePart.store
@activeStores = Store.scoped_by_deactivated(false)
@storesSpareParts = StoresSparePart.scoped_by_store_id(@currentStore.id)
Rails.logger.debug { @currentStore.title }
Rails.logger.debug { @storesSpareParts.count }
render 'create.js'
else
render 'create.js'
end
end
Here is the POST request passed to the create method:
Started POST "/stores_spare_parts" for 127.0.0.1 at 2011-05-02 17:52:00 +0200
Processing by StoresSparePartsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"+NKTAC8ibgUW8vYg5H0XcWB+hAoFdw6on3Uw2XfP9WQ=",
"stores_spare_part"=>{"spare_part_id"=>"6", "quantity"=>"2",
"lowMark"=>"", "store_id"=>"1"}, "commit"=>"Create Stores spare part"}
Upvotes: 0
Views: 566
Reputation: 2648
Change post :update_attribute_on_the_spot
to put :update_attribute_on_the_spot
since it looks like you are updating a record (put) and not creating a record (post). I am pretty sure RoR got mad at me one time for doing something like what you did.
Upvotes: 1