有没有办法使用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) 我有一个叫做车辆的类,它可以附加一个图像。如果在 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)