2015-08-15 91 views
1

收到此錯誤:Ruby on Rails的:創建刪除鏈接關聯對象

undefined method `to_model' for #<Paperclip::Attachment:0x007fc5d1c46e60> 

我試圖創建一個刪除鏈接到我的相關圖像

<% @project.project_images.each do |image| %> 
    <%= image_tag image.photo.url(:thumb) %> 
    <div class="actions"> 
    <%= link_to "remove", image.photo, confirm: "Are you sure?", method: :delete %> 
<% end %> 

我的模型

class Project < ActiveRecord::Base 
    has_many :project_images, dependent: :destroy 
    accepts_nested_attributes_for :project_images, allow_destroy: true 
end 

class ProjectImage < ActiveRecord::Base 
    belongs_to :project 
end 

我相信我的image.photo是錯的,但我不確定它應該是什麼?我想我應該確定路徑是什麼?但是我沒有ProjectImages的路線。我只通過嵌套屬性保存圖像。我真的需要創建一個新的路線嗎?如果是這樣,它會是什麼?

編輯(添加路由)

這是我的路線:

resources :projects do 
    member do 
    get 'add_photos' 
    post 'upload_photos' 

    end 
end 
+0

您可以發佈您耙路由的詳細信息。 –

+0

@bipashant嗨,我說我的路線,這是什麼在我的路線,涉及到項目 – hellomello

回答

2
<% @project.project_images.each do |image| %> 
    <%= image_tag image.photo.url(:thumb) %> 
    <div class="actions"> 
    <%= link_to "remove", project_path(@project.id, project: { project_images: { id: image.id, "_destroy" => true }}), remote: true, confirm: "Are you sure?", method: :put %> 
    </div> 
<% end %> 
+0

您好!我得到這個錯誤:''undefined method'project_image_path'for#<#:0x007fb005308970>' – hellomello

+0

請試試這個 link_to「remove」,project_path(@project_id,project:{project_images:{ id:image.id,「_destroy」=> true}}),remote:true,confirm:「Are you sure?」,method :: put –

+1

http://stackoverflow.com/questions/7150329/adding-a- delete-link-for-nested-attributes –

-1

剛剛在的link_to刪除.photo,因爲它不是一個對象,這是一個範圍界定方法與photo工作(預覽等):

<% @project.project_images.each do |image| %> 
    <%= image_tag image.photo.url(:thumb) %> 
    <div class="actions"> 
    <%= link_to "remove", image, confirm: "Are you sure?", method: :delete %> 
    </div> <!-- maybe, you forgot it? --> 
<% end %> 
+0

我刪除了它,現在我得到這個錯誤:'未定義的方法「project_image_path」爲#<#<類別:0x007fc5cfff1be8>:0x007fc5ce1db050 >' – hellomello

+0

這不是範圍的錯誤。請向我們顯示行,這會導致錯誤,並向我們顯示您的'config/routes.rb' – asiniy

+0

它顯示在這行''%= link_to「remove」,圖像,確認:「你確定嗎?」,方法: :刪除%>'。我還添加了與項目相關的路線 – hellomello