附件只传递文件名 - Rails 4
问题描述:
我想在Rails 4中以嵌套形式传递一个文档,但参数只取附件名。我已经做了一些观察,人们遇到的大多数问题都是因为他们遗漏了我在代码中的:multipart => true
。我有两个fields_for
在相同的形式(即两个孩子形式)我不确定这是否可能是问题的一部分。附件只传递文件名 - Rails 4
这是我的表格。我要离开了一些属性的div,错误消息为简洁起见
= form_for([@auction, @bid], html: {class: ''})do |f|
= f.fields_for :inventory_part, InventoryPart.new do |cm|
.form-group
.col-lg-6
%label.col-lg-4.control-label Part No.
= cm.text_field :part_num
%label.col-lg-4.control-label Manufacturer
= cm.text_field :manufacturer
.form-group
%label Serial No.
= cm.text_field :serial_num
%label Condition
.inv-radio-btns
.right-radios
.radio
= cm.radio_button :condition, "recent"
= cm.label :condition, "NE"
.radio
= cm.radio_button :condition, "overhaul"
= cm.label :condition, "OH"
.radio
= cm.radio_button :condition, "as_removed"
= cm.label :condition, "AR"
.form-group
%label Amount:
= f.text_field :part_price
.form-group
%label Shipping Est:
= f.text_field :est_shipping_cost
.form-group
%label Documentation:
= f.fields_for :document, Document.new, html: { :multipart => true, class: "form-control"} do |dm|
.files
%label.btne
Browse
= dm.file_field :attachment
= f.submit 'Submit Quote', class: 'btn btn-defualt bg-success pull-right'
的要求,控制器动作:
def temp_user_create_bid
Bid.strip_symbols(bid_params)
@bid = @auction.bids.new(bid_params)
@inventory_part = InventoryPart.new(inventory_part_params)
part_match = Part.find_by(part_num: @inventory_part.part_num.upcase)
respond_to do |format|
if part_match
@bid.inventory_part = @inventory_part
if @bid.save
@inventory_part.add_part_details(part_match, current_user)
if @inventory_part.save
document_params[:attachment].each { |doc| @bid.documents.create(name: doc.original_filename, attachment: doc)} if document_params
Notification.notify_other_bidders(@auction, current_user, "A quote has been placed on an RFQ you are participating in!")
Notification.notify_auctioner(@auction, "A new quote was placed in your RFQ!")
format.html { redirect_to @bid.auction, notice: 'Your quote has been saved' }
else
format.html { render :temp_user_new_bid }
end
elsif !part_match
flash[:error] = "Part number is not valid"
format.html { redirect_to temp_user_new_bid(@bid.auction), alert: 'Part Number was not valid.' }
else
flash[:error] = @bid.errors.full_messages.to_sentence.gsub('.','')
format.html { redirect_to new_auction_bid_path(@auction) }
format.json { render json: @bid.errors, status: :unprocessable_entity }
end
end
end
end
和PARAMS
{"utf8"=>"✓", "authenticity_token"=>"wKAxNdL0rRdN5iHUMHEw7lBl6gkdWK+MDddlWUsGIYdXVkcTMvT/hNt0/45Y6SYxXf0A8p5LY/6IsiK68Jtj2A==",
"bid"=>{
"inventory_part"=>{
"part_num"=>"123", "manufacturer"=>"Textron",
"serial_num"=>"2342", "condition"=>"overhaul"
},
"part_price"=>"2344.44", "est_shipping_cost"=>"203.00",
"quantity"=>"1", "tag_date"=>"2017-05-23", "reference_num"=>"34ko33",
"document"=>{
"attachment"=>"Second EIN.pdf"
}
},
"commit"=>"Submit Quote", "controller"=>"bids",
"action"=>"temp_user_create_bid", "auction_id"=>"113"}
答
请使用file_field_tag
而不是file_field
并检查它是否有效。
答
我已经做了一些前瞻性和大部分问题的人都是 经历了,因为他们离开了:多=>真的,我 在我的代码。
你有它在你的代码,但在错误的地方。您应该通过:multipart => true
作为html选项为form_for
而不是fields_for
接受附件。
= form_for([@auction, @bid], html: {:multipart => true, class: ''}) do |f|
,并从fields_for
= f.fields_for :document, Document.new, html: {class: "form-control"} do |dm|
+0
不幸的是,这种改变并没有解决我的问题。只有该文件的名称仍然被传递,因为“附件” – gemart
它仍然只传递文件的名称中删除了':attachment' – gemart
OK,你可以分享你的控制器的代码? –
控制器的动作并不漂亮,它是新的,我没有重构..但它是 – gemart