Carrierwave文件没有上传Rails4

时间:2016-01-10 18:03:19

标签: ruby-on-rails ruby-on-rails-4 carrierwave

除了存储的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

型号:     class Post&lt;的ActiveRecord ::基

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&gt;

表格:(最后使用文件字段标签)

<%= 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秒。

2 个答案:

答案 0 :(得分:0)

也许存在与远程提交多部分表单相关的问题,您可以尝试这个gem,它在这种情况下帮助过我:Advanced Content Filter

更新:

当您远程提交表单时,您应该在操作中呈现json而不是默认行为:

config.allowedContent =
    'h1 h2 h3 p blockquote strong em;' +
    'a[!href];' +
    'img(left,right)[!src,alt,width,height];';

答案 1 :(得分:0)

我切换到from_for及其工作,也许我使用的form_tag语法存在一些问题。 谢谢@hedgesky试图帮助我。