多个图像上传的问题PaperClip,嵌套表格Rails 5

时间:2017-07-20 01:35:06

标签: ruby-on-rails ruby paperclip

我已经完成了几个问题(主要是14880100&amp; 2308103),并且无法弄清楚如何将多个图片上传到我的Rails模型。< / p>

这是我的代码:

photo.rb

class Photo < ApplicationRecord
attr_accessor :location_id, :name, :picture
belongs_to :location
has_attached_file :picture,
                styles: { large: "", medium: "300x300#", thumb: "100x100#" },
                storage: :s3,
                :s3_protocol => :https,
                url: ":s3_domain_url",
                default_url: "placeholder.jpg",
                path: "/:class/:attachment/:id_partition/:style/:filename",
                s3_region: ENV["S3_REGION"],
                s3_credentials: Proc.new { |a| a.instance.s3_credentials }




def s3_credentials
{
  bucket: ENV['S3_BUCKET_NAME'],
  access_key_id: ENV['S3_ACCESS_KEY_ID'],
  secret_access_key: ENV['S3_SECRET_ACCESS_KEY'],
}
 end
end

location.rb

class Location < ApplicationRecord
  geocoded_by :address
  after_validation :geocode
  acts_as_votable
  extend FriendlyId
  friendly_id :title, use: :slugged
  has_many :photos
  accepts_nested_attributes_for :photos, :allow_destroy => true
  belongs_to :user
  has_many :comments, dependent: :destroy


end

的routes.rb

 resources :locations do
  resources :photos
  resources :comments
    member do
      put "like" => "locations#upvote"
      put "dislike" => "locations#downvote"
    end
 end

/views/locations._form.html.erb

<div class="form-group">
 <div class="col-md-5">
  <%= nested_form_for @location, :html => { :multipart => true } do |f| %>
  <%# all your building fields .... %>

  <%= f.fields_for :photos do |photo| %>
    <% if photo.object.new_record? %>
      <%= photo.file_field(:picture) %>
    <% else %>
      <%= image_tag(photo.url(:thumb)) %>
      <%= photo.hidden_field :_destroy %>
      <%= photo.link_to_remove "X" %>
    <% end %>
  <% end %>

  <p><%= f.link_to_add "Add photo", :photos %></p>

<% end %></div>

/views/locations.show.html.erb

<%= image_tag @location.photos.picture.url %>

我收到的错误是&#34;未定义的方法`picture&#39;&#34;。

任何提示都将不胜感激。

1 个答案:

答案 0 :(得分:1)

代码@location.photos将返回一个数组,你必须迭代它,这是代码:

<% @location.photos.each do |photo| %>
  <%= image_tag(photo.picture.url)%>
<% end %>
相关问题