问题描述
当 current_user
单击图钉时,如何将图像保存到 current_user
?
When the current_user
clicks the pin how can that image be saved to the current_user
?
按钮:
<%= 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 %>
输出:
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,
固定图像的 id
不同,给出错误:没有路线匹配[GET] /system/inspirations/images/000/000/002/medium/choose-optimism.png
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"
工作映像具有 001
:
/ system / inspirations / images / 000/000/001 / medium / choose-optimism.png?1478840469
解决此问题的最佳方法是什么,我可以继续传递不同的ID,但使用同一张图片?
What's the best way to fix this so I can continue to pass different IDs, but use the same image?
控制器
@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
推荐答案
在您的模型中:
has_attached_file :image, url: '/system/inspirations/images/:style/:filename'
但是,执行此操作后,您将必须重新创建首先是灵感,因此它将使用新的URL插值。
However, after you do this, you'll have to recreate your first Inspiration so it will use new URL interpolation.
这篇关于没有路线与[GET]的固定图片匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!