Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 3:仅在特定方法中存在路由错误_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails Rails 3:仅在特定方法中存在路由错误

Ruby on rails Rails 3:仅在特定方法中存在路由错误,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我在routes.rb中遇到某个记录的奇怪行为。以下是my routes.rb的摘录: resources :stores do resources :stores_spare_parts do collection do post :update_attribute_on_the_spot end end end 现在,当我在stores\u spare\u parts Controller的索引操作中为调用方法url\u时,它返回http://127.0

我在routes.rb中遇到某个记录的奇怪行为。以下是my routes.rb的摘录:

resources :stores do
  resources :stores_spare_parts do
    collection do
      post :update_attribute_on_the_spot
    end
  end
end
现在,当我在stores\u spare\u parts Controller的索引操作中为调用方法
url\u时,它返回
http://127.0.0.1:3000/stores/1/stores_spare_parts/update_attribute_on_the_spot
。但当我在创建操作中执行相同操作时,它会停止工作,并出现以下错误:

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'
以下是索引和创建方法的控制器代码:

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

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
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\u attribute\u on\u the \u spot
put:update\u attribute\u on\u the \u spot
,因为看起来您正在更新记录(put),而不是创建记录(post)。我敢肯定RoR有一次因为我做了你所做的事情而对我生气。

你给你的
url\u传递了什么参数来进行
通话?。。。您能否在相关操作中提供完整的控制器代码?更新\u属性\u在\u站点上\u是否真的应该是一个“集合”?你能描述一下它的用途吗?我刚刚添加了相应的控制器代码。on_the_spot是视图中就地编辑的瑰宝。我根据自述文件对其进行了配置:您确定浏览器请求确实是POST请求吗?如果不是,它会使用于
查找的
url\u不工作吗?不幸的是,它没有帮助。无论我是否更改或添加put行,我都会得到相同的错误。@Daniel不管怎样,它应该是
put
。。。甚至gem文档也说要使用
put
。这就是宁静的工作方式。接下来,我会仔细检查并确保您将
gem“放在gemfile内部的\u点上”
,然后在\u点上运行
rails g:install
。我没有提到的是(因为这不是我的问题),在\u点上使用索引方法。它仅在调用create方法时失败。我将错误缩小到函数的url_。这就是我问这个问题的原因。关于实际现场问题的问题在这里:更改我的post=>put使我的错误得以避免(在rails 3.0=>3.1端口期间)