将每个属性与除图像之外的对象相关联的表单

问题描述 投票:0回答:1

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

意见/汽车/ _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> 
ruby-on-rails paperclip
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.