【问题标题】:Attachment Param Only Passing Name of File - Rails 4附件参数仅传递文件名称 - Rails 4
【发布时间】:2017-05-31 03:24:09
【问题描述】:

我试图在 Rails 4 中以嵌套形式传递文档,但参数仅采用附件名称。我做了一些查找,人们遇到的大多数问题是因为他们遗漏了我在代码中的:multipart => true。我有两个 fields_for 在同一个表单中(即两个子表单)我不确定这是否是问题的一部分。

这是我的表格。为简洁起见,我省略了一些属性、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

和参数

{"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"}

【问题讨论】:

    标签: ruby-on-rails forms ruby-on-rails-4 attachment nested-forms


    【解决方案1】:

    请使用file_field_tag 而不是file_field 并检查它是否有效。

    【讨论】:

    • 它仍然只传递文件名作为:attachment
    • 控制器动作不漂亮,它是新的,我还没有重构..但它就在那里
    • 我也添加了参数
    【解决方案2】:

    我做了一些查找,大多数问题都是人们遇到的 经历是因为他们遗漏了 :multipart => true 我 在我的代码中有。

    您的代码中有它,但是在错误的地方。您应该将 :multipart => true 作为 html 选项 传递给 form_for not 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|
    

    【讨论】:

    • 不幸的是,此更改无法解决我的问题。只有文件名仍在传递,作为“附件”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 2011-07-06
    • 1970-01-01
    • 2013-04-02
    • 2016-06-21
    • 1970-01-01
    • 2013-02-24
    相关资源
    最近更新 更多