2015-08-15 93 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