AnthonyGalli.com AnthonyGalli.com - 28 days ago 9
Ruby Question

No route matches [GET] for Pinned Image

When the

current_user
clicks the pin how can that image be saved to the
current_user
?

enter image description here

Button:

<%= simple_form_for(@inspiration) do |f| %>
<%= f.hidden_field :image_file_name, value: inspiration.image_file_name %>
<%= f.hidden_field :image_content_type, value: inspiration.image_content_type %>
<%= f.hidden_field :image_file_size, value: inspiration.image_file_size %>
<%= button_tag(type: 'submit', class: "btn btn-primary") do %>
<span class="glyphicon glyphicon-pushpin"></span>
<% end %>
<% end %>


Outputs:

pry(main)> Inspiration.first
id: 1,
user_id: 1,
image_file_name: "choose-optimism.png",
image_content_type: "image/png",
image_file_size: 230082,
pry(main)> Inspiration.last
id: 2, # Creating the issue of the route error & image not rendering
user_id: 2,
image_file_name: "choose-optimism.png",
image_content_type: "image/png",
image_file_size: 230082,


The
id
is different for the pinned image giving the error:
No route matches [GET] "/system/inspirations/images/000/000/002/medium/choose-optimism.png"


The working image has
001
:

/system/inspirations/images/000/000/001/medium/choose-optimism.png?1478840469


What's the best way to fix this so I can continue to pass different IDs, but use the same image?

controller

@inspirations = @user.inspirations # Renders Images with Pin Overlayed
@inspiration = current_user.inspirations.build # For the Button

def create
@inspiration = current_user.inspirations.build(inspiration_params)
@inspiration.save
respond_modal_with @inspiration
end

Answer

https://github.com/thoughtbot/paperclip/wiki/Interpolations

In your model:

has_attached_file :image, url: '/system/inspirations/images/:style/:filename'

However, after you do this, you'll have to recreate your first Inspiration so it will use new URL interpolation.