Rails 3:仅在特定方法中出现路由错误 [英] Rails 3: Routing error only in a certain method
问题描述
我的 routes.rb 中的某个记录出现了奇怪的行为.这是我的 routes.rb 的摘录:
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
当我现在在我的 store_spare_parts 控制器的索引操作中调用方法 url_for
时,它返回 http://127.0.0.1:3000/stores/1/stores_spare_parts/update_attribute_on_the_spot代码>.但是,当我在创建操作中执行相同操作时,它会因以下错误而停止工作:
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
这是传递给 create 方法的 POST 请求:
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"}
推荐答案
将 post :update_attribute_on_the_spot
更改为 put :update_attribute_on_the_spot
因为看起来您正在更新记录(put) 而不是创建记录 (post).我很确定 RoR 有一次因为我做了你所做的事情而生我的气.
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.
这篇关于Rails 3:仅在特定方法中出现路由错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!