简体   繁体   English

Rails 3:仅在某种方法中出现路由错误

[英]Rails 3: Routing error only in a certain method

I am experiencing a strange behavior for a certain record in my routes.rb.我的 routes.rb 中的某个记录出现了奇怪的行为。 Here is an excerpt of my routes.rb:这是我的 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 .当我现在在 stores_spare_parts Controller 的索引操作中调用方法url_for时,它返回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:这是索引和创建方法的 controller 代码:

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:这是传递给 create 方法的 POST 请求:

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"}

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).post:update_attribute_on_the_spot更改为put:update_attribute_on_the_spot ,因为看起来您正在更新记录 (put) 而不是创建记录 (post)。 I am pretty sure RoR got mad at me one time for doing something like what you did.我很确定 RoR 有一次因为我做了你所做的事情而生我的气。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM