簡體   English   中英

載波文件未上傳Rails4

[英]Carrierwave files not uploading Rails4

一切正常,除了存儲的image1字符串為nil。 有沒有辦法找到載波產生的誤差。

這是控制器

def create_post

@post = Post.new(:club => params[:club],:venue => params[:Venue], :title => params[:title] , :shortdesc => params[:shortdesc],:longdesc => params[:longdesc] ,:category => params[:category],:eventdatetime => params[:eventdatetime],:video1 => params[:video1],:file1 => params[:file1],:file2 => params[:file2],:file3 => params[:file3])
    @post.image1 = params[:image1]
    if @post.save
        redirect_to '/'+params[:club]
    else
        redirect_to '/admin'
        flash[:notice] = "Post could not be saved at the moment, try later"
    end

end

上載者:

class Image1Uploader < CarrierWave::Uploader::Base
storage :file
def store_dir
 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

模型:類Post <ActiveRecord :: Base

mount_uploader :image1, Image1Uploader
validates_integrity_of :image1
validates_processing_of :image1
validates_download_of :image1
end

記錄已插入:

 Post Load (0.3ms)  SELECT  `posts`.* FROM `posts` WHERE `posts`.`id` = ? LIMIT 1  [["id", 49]] => #<Post id: 49, club: "Joy", title: "", date: nil, timr: nil, image2: nil, image3: nil, video1: "", video2: nil, file1: "", file2: "", file3: "", shortdesc: "", longdesc: "", category: nil, created_at: "2016-01-10 17:35:54", updated_at: "2016-01-10 17:35:54", eventdatetime: "2016-01-15 23:00:00", venue: "", image1: nil>

2.2.1:006>

表格:(最后使用文件字段標簽)

<%= form_tag action: "create_post" ,method: :post, multipart: true , remote: true do  %>

  <div class="form-group">
    <label for="inputTitle" class="col-sm-2 control-label">Title</label>
    <div class="col-sm-7">
     <%= text_field_tag(:title,nil, :class =>'form-control', :placeholder => "Title of the post", :maxlength =>30) %>
     <p>Max. 30 characters</p>
    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-2 control-label">Category</label>
    <div class="col-sm-7">
      <div class="radio">

        <label>
          <%= radio_button_tag 'category', 'Event' %>
          Event
        </label><br>

        <label>
          <%= radio_button_tag 'category', 'Workshop' %>
          Workshop
        </label><br>

        <label>
          <%= radio_button_tag 'category', 'Induction' %>
          Induction
        </label><br>

        <label>
          <%= radio_button_tag 'category', 'Others' %>
          Others
        </label>

      </div>
    </div>
  </div>

  <div class="form-group">
    <label for="inputVenue" class="col-sm-2 control-label">Venue</label>
    <div class="col-sm-7">
      <%= text_field_tag(:Venue ,nil, :class =>'form-control', :placeholder => "Venue", :maxlength =>15) %>
      <p>Max. 15 characters</p>
    </div>
  </div>

  <div class="form-group">
    <label for="inputDate" class="col-sm-2 control-label">Date and Time</label>
    <div class="col-lg-4"> 

        <%= text_field_tag(:eventdatetime ,Time.now.strftime("%Y-%m-%d %H:00"), :class =>'form-control', :maxlength =>20) %>
        <p>(YYYY-MM-DD HH:MM)</p>

    </div> 


    </div>

  <div class="form-group">
    <label for="shortDescription" class="col-sm-2 control-label">Short Description</label>
    <div class="col-sm-7">
      <%= text_area_tag(:shortdesc,nil, :class =>'form-control', :placeholder => "Short Description of the post", :maxlength => 140, :rows=> 3) %>
      <p>Max. 140 characters</p>
    </div>
  </div>

  <div class="form-group">
    <label for="longDescription" class="col-sm-2 control-label">Long Description</label>
    <div class="col-sm-7">
      <%= text_area_tag(:longdesc,nil, :class =>'form-control', :placeholder => "Complete description of the Event", :maxlength =>2000, :rows=> 5) %></textarea>
    <p>No word limit</p>
    </div>
  </div>

  <div class="form-group">
    <label for="insert-image" class="col-sm-2 control-label">Insert Image</label>
    <div class="col-sm-7">
      <%= file_field_tag :image1 %>
    </div>
  </div>

日志:

Started POST "/admin/create_post?method=post&multipart=true&remote=true" for 117.211.90.154 at 2016-01-11 13:48:40 +0530
Cannot render console from 117.211.90.154! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by AdminController#create_post as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"hcv/exnjSx9Ry2lg1CUs9cUHoF+hMMo0f4A5pkf7/XxTcZx9MpBLCdGb81k/ajAuMYC4xtPBWzFl+O1Sk1vf/w==", "title"=>"Title", "category"=>"Induction", "Venue"=>"Venue", "eventdatetime"=>"2016-01-12 13:00", "shortdesc"=>"", "longdesc"=>"", "image1"=>"Capture.JPG", "video1"=>"", "file1"=>"", "file2"=>"", "file3"=>"", "club"=>"Joy", "method"=>"post", "multipart"=>"true", "remote"=>"true"}
  [1m[35mSQL (0.1ms)[0m  BEGIN
[1m[36mSQL (0.2ms)[0m  [1mINSERT INTO `posts` (`club`, `venue`, `image1`, `title`, `shortdesc`, `longdesc`, `category`, `eventdatetime`, `video1`, `file1`, `file2`, `file3`, `created_at`, `updated_at`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)[0m  [["club", "Joy"], ["venue", "Venue"], ["image1", nil], ["title", "Title"], ["shortdesc", ""], ["longdesc", ""], ["category", "Induction"], ["eventdatetime", "2016-01-12 13:00:00"], ["video1", ""], ["file1", ""], ["file2", ""], ["file3", ""], ["created_at", "2016-01-11 08:18:40"], ["updated_at", "2016-01-11 08:18:40"]]
[1m[35m (2.7ms)[0m  COMMIT
Redirected to http://bitdeets.in/Joy
Completed 302 Found in 17ms (ActiveRecord: 3.6ms)

文件似乎沒有上傳,因為在幾秒鍾內加載了下一頁,並且文件大小足夠大,至少需要30秒才能上傳。

也許存在與遠程提交多部分表單有關的問題,您可以嘗試使用此gem,在這種情況下它對我有幫助: https : //github.com/JangoSteve/remotipart

更新:

在遠程提交表單時,應在操作中呈現json而不是默認行為:

def create_post
  @post = Post.new(:club => params[:club],:venue => params[:Venue], :title => params[:title] , :shortdesc => params[:shortdesc],:longdesc => params[:longdesc] ,:category => params[:category],:eventdatetime => params[:eventdatetime],:video1 => params[:video1],:file1 => params[:file1],:file2 => params[:file2],:file3 => params[:file3])
  @post.image1 = params[:image1]
  if @post.save
    render json: {post: @post, error: false}
  else
    render json: {error: true}
  end
end

我切換到from_for及其工作,也許我使用的form_tag語法存在一些問題。 感謝@hedgesky嘗試幫助我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM