1

Getting this error:

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

I'm trying to create a remove link to my associated image

<% @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 %>

My Model

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

I believe my image.photo is wrong, but I'm not sure what it should be? I guess I should determine what the path is? But I don't have a routes for ProjectImages. I'm only saving the images through nested attributes. Do I really need to create a new routes? If so, what would it be?

EDIT (Adding Routes)

This is my routes:

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

  end
end
2
  • Can you post your rake routes details. Commented Aug 15, 2015 at 18:31
  • @bipashant Hi, I added my routes, this is whats in my routes that pertains to projects Commented Aug 15, 2015 at 21:54

2 Answers 2

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 %>
Sign up to request clarification or add additional context in comments.

6 Comments

Hello! I'm getting this error: undefined method 'project_image_path' for #<#<Class:0x007fb005320c50>:0x007fb005308970>
Please try this link_to "remove", project_path(@project.id, project: { project_images: { id: image.id, "_destroy" => true }}), remote: true, confirm: "Are you sure?", method: :put
I'm able to implement the code, but nothing has happened?
Okay, I figured it out.. kinda... still need help though. So instead of project_images:, I did project_images_attributes: but it doesn't pop up a message, because of the remote: true. So I only know it was deleted, when I refresh. If I take this remote: true off, it gets redirected to the show page. I just want to keep it in the same page where this delete photo is. Its not confirming if I wanted to delete the photo or not.
|
-1

Just remove .photo in the link_to, because of it's not an object, this is a scope to define methods to work with photo (previews and so on):

<% @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 %>

3 Comments

I removed it and now I'm getting this error: undefined method 'project_image_path' for #<#<Class:0x007fc5cfff1be8>:0x007fc5ce1db050>
It isn't error of it's scope. Please show us line, which cause the error, plus show us your config/routes.rb
Its showing it on this line <%= link_to "remove", image, confirm: "Are you sure?", method: :delete %>. I also added my routes that pertains to projects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.