2

I am getting a Rails error when i create a new "car_image"... the AJAX response is

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

Route defined

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

Rake Routes

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

However, the route is setup and i can see it when i rake routes, so i am not sure what the issue is. I am using the route in my model method:

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

What would be causing the undefined method here? The record does save, but i get the error response...

4

2 回答 2

2

由于您需要模型中的链接(不应该),请在其中添加:

delegate :url_helpers, to: 'Rails.application.routes'

然后替换:

car_image_path(:id => id)

和:

url_helpers.car_image_path(self)
于 2013-05-02T16:03:28.300 回答
0

您没有:show在资源上定义方法,因此它不会创建show路由,也不会为您提供car_image_path方法。

如果你想要这个car_image_path方法(它需要一个参数,它应该是你想要路径的汽车图像),将路由更改为如下所示:

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

但是,如果您只是在寻找所有汽车图像的路径,car_images_path那就是您正在寻找的东西。

于 2013-05-02T17:12:03.790 回答