小编her*_*nte的帖子

如何在提交前向表单添加其他字段?

有没有办法使用javascript和JQuery添加一些使用POST从HTTP表单发送的其他字段?

我的意思是:

<form action="somewhere" method="POST" id="form">
  <input type="submit" name="submit" value="Send" />
</form>

<script type="text/javascript">
  $("#form").submit( function(eventObj) {
    // I want to add a field "field" with value "value" here
    // to the POST data

    return true;
  });
</script>
Run Code Online (Sandbox Code Playgroud)

html forms jquery post field

95
推荐指数
6
解决办法
17万
查看次数

使用活动存储在 rails 中的 seed.rb 文件中附加图像

我有一个叫做车辆的类,它可以附加一个图像。如果在 Vehicle.rb 文件中没有上传其他图像,我已经制作了一个默认图像来显示。

我想在seed.rb 文件中包含图像,这样我就不必手动上传所有图像。这可能吗?

非常感谢帮助。

这是我的车辆.rb:

class Vehicle < ApplicationRecord
  belongs_to :make
  belongs_to :model
  accepts_nested_attributes_for :make
  accepts_nested_attributes_for :model
  has_one_attached :image

  after_commit :add_default_image, on: %i[create update]

  def add_default_image
    unless image.attached?
      image.attach(
        io: File.open(Rails.root.join('app', 'assets', 'images', 'no_image_available.jpg')),
        filename: 'no_image_available.jpg', content_type: 'image/jpg'
      )
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这是我在种子文件中创建记录的方式,但我也想包含图像:

v = Vehicle.create(
  vin: '1QJGY54INDG38946',
  color: 'grey',
  make_id: m.id,
  model_id: mo.id,
  wholesale_price: '40,000'
)
Run Code Online (Sandbox Code Playgroud)

ruby rails-activestorage

6
推荐指数
2
解决办法
3033
查看次数

标签 统计

field ×1

forms ×1

html ×1

jquery ×1

post ×1

rails-activestorage ×1

ruby ×1