将每个属性与除图像

时间:2018-03-02 01:44:28

标签: ruby-on-rails paperclip

我正在使用回形针上传图像并将它们与带有轨道的对象相关联,而我无法弄清楚为什么每个属性都与图像中的正确对象相关联。以下是相关文件:

views / cars / _form

<%= form_for @car do |f| %>

<% if @car.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@car.errors.count, "error") %> prohibited this car from being saved:</h2>

    <ul>
    <% @car.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

  <%= f.label :make %>
  <%= f.text_field :make %><br>

  <%= f.label :model %>
  <%= f.text_field :model %><br>

  <%= f.label :color %>
  <%= f.text_field :color %><br>

  <%= f.label :year %>
  <%= f.text_field :year %><br>

  <%= f.label :image %>
  <%= f.file_field :image %>

  <%= f.submit %>

<%end%>

car.rb

class Car < ApplicationRecord
  attr_accessor :image_file_name

  belongs_to :user
 has_many :categories, through: :awards
  validates :year, length: {is: 4}
  has_attached_file :image,
                  styles: {
                      thumb: ["300x300#", :jpeg],
                      original: [:jpeg]
                  },
                  convert_options: {
                      thumb: "-quality 70 -strip",
                      original: "-quality 90"
                  }

  validates_attachment :image,
                     content_type: { content_type: /\Aimage\/.*\z/ },
                     size: { less_than: 4.megabyte }

  scope :classic, ->(time) { where("cars.year < ?", time)}

end

我已经看了一遍,还没有找到这个问题的任何实例。品牌,型号,颜色和年份都与新车对象相关联,但图像缺失。有谁知道为什么会这样?提前谢谢!

编辑:这是我创建的最后一个对象,看起来它只是将文件名与图像相关联。嗯...

2.3.3 :001 > Car.last
  Car Load (0.9ms)  SELECT  "cars".* FROM "cars" ORDER BY "cars"."id" DESC LIMIT ?  [["LIMIT", 1]]
 => #<Car id: 5, make: "Honda", model: "Civic", year: 2002, color: "Black", user_id: 1, image_file_name: nil, image_content_type: "image/jpeg", image_file_size: 49575, image_updated_at: "2018-03-02 01:04:21", created_at: "2018-03-02 00:29:26", updated_at: "2018-03-02 01:04:21", image: nil> 

1 个答案:

答案 0 :(得分:0)

car.rb中的attr_accessor :image_file_name不允许文件名与创建的对象关联。删除后,保存图像并在提交表单时与对象关联。