我想在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"}
它仍然只傳遞文件的名稱中刪除了':attachment' – gemart
OK,你可以分享你的控制器的代碼? –
控制器的動作並不漂亮,它是新的,我沒有重構..但它是 – gemart