Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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 _路径的未定义方法,但存在路由_Ruby On Rails_Activerecord_Model_Routing - Fatal编程技术网

Ruby on rails _路径的未定义方法,但存在路由

Ruby on rails _路径的未定义方法,但存在路由,ruby-on-rails,activerecord,model,routing,Ruby On Rails,Activerecord,Model,Routing,当我创建一个新的“汽车图像”时,我遇到了一个Rails错误。。。AJAX响应是 undefined method `car_image_path' for #<CarImage:0x007fdbb1b79258> 耙道 car_images GET /car_images(.:format) car_images#index POST /car_images(.:format) car_images#cr

当我创建一个新的“汽车图像”时,我遇到了一个Rails错误。。。AJAX响应是

undefined method `car_image_path' for #<CarImage:0x007fdbb1b79258>
耙道

car_images GET    /car_images(.:format)                  car_images#index
POST   /car_images(.:format)                  car_images#create
car_image DELETE /car_images/:id(.:format)              car_images#destroy
但是,路线是设置好的,我可以在搜索路线时看到它,所以我不确定问题出在哪里。我正在使用模型方法中的路线:

class CarImage < ActiveRecord::Base
  belongs_to :car

  attr_accessible :description, :image, :title, :car_id, :file

  mount_uploader :file, CarImageUploader

  def to_jq_upload
    {
      "name" => read_attribute(:file),
      "size" => file.size,
      "url" => file.url,
      "thumbnail_url" => file.thumb.url,
      "delete_url" => car_image_path(:id => id),
      "delete_type" => "DELETE" 
    }
  end

end
class-CarImageread_属性(:file),
“size”=>file.size,
“url”=>file.url,
“缩略图url”=>file.thumb.url,
“删除url”=>car\u image\u路径(:id=>id),
“删除类型”=>“删除”
}
结束
结束

是什么导致了这里的未定义方法?记录确实保存了,但我得到了错误响应…

因为您需要模型中的链接(不应该是),请在其中添加以下内容:

delegate :url_helpers, to: 'Rails.application.routes'
然后替换:

car_image_path(:id => id)
与:


您没有在资源上定义
:show
方法,因此它不会创建
show
路线,也不会为您提供
car\u image\u path
方法

如果要使用
car\u image\u path
方法(该方法需要一个参数,该参数应该是您要为哪个car image设置路径),请将路由更改为如下所示:

resources :car_images, :only => [:index, :show, :create, :destroy]

然而,如果你只是在寻找所有汽车图像的路径,
汽车图像路径
就是你要寻找的。

我看不到路线我编辑的问题显示了rake路线打印输出哦,你在一个模型中……这里有没有更好的方法可以推荐?也许只是硬编码路径,而不是使用URL帮助器呢?我想我知道一个更好的方法,在我的视图代码中,我使用file.delete\u URL,而不是在视图中使用URL\u helper路径,car\u image\u path(file.id)——想法??不,永远不要硬编码路径!为了维护起见。我理解您的需要,我已经为此制作了一个专用类,使用与上面相同的委托我需要这个来使用application helpersim中的路由路径使用destroy路径,这与show相同,方法param correct?我已经定义了:销毁路径。。。所以我不明白为什么这条路线会显示出来,但是这个方法不起作用
url_helpers.car_image_path(self)
resources :car_images, :only => [:index, :show, :create, :destroy]