0

以下是提交表单时服务器窗口中显示的内容:

 Started POST "/seniors" for 127.0.0.1 at 2013-08-21 17:27:34 -0400
Processing by SeniorsController#create as HTML
  Parameters: {"utf8"=>"√", "authenticity_token"=>"zWeveRhstI178IawtOka6TV2wdKwnpKq2J74VYYNf1U=", "senior"=>{"first_name"=>"Jane", "last_name"=>"dOE", "employer _id"=>"1"}, "commit"=>"Create Senior"}
   (0.0ms)  begin transaction
  SQL (3.0ms)  INSERT INTO "seniors" ("created_at", "employer_id", "first_name", "image", "last_name", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["created_at", Wed, 21 Aug 2013 21:27:34 UTC +00:00], ["employer_id", 1], ["first_name", "Jane"], ["image", nil], ["last_name", "dOE"], ["updated_at", Wed , 21 Aug 2013 21:27:34 UTC +00:00]]
   (107.7ms)  commit transaction
Redirected to http://localhost:3000/seniors
Completed 302 Found in 120ms (ActiveRecord: 110.7ms)

表格:

    <%=content_for :page do%>
    <div class="menuTitle"><h1><%if @senior.new_record?%>New<%else%>Edit<%end%> Senior</h1></div>
    <%=form_for @senior, :html=>{:multipart => true} do |f|%>
        <div class="login form">
        #<p>First Name: <%=f.text_field :first_name%></p>
        <p>Last Name: <%=f.text_field :last_name%></p>
        <p>Facility: <%=f.collection_select :employer_id, @employers, :id, :name%></p>
        <p>Image: <%=f.file_field :image%></p>
        <p><%=f.submit :class => "button red"%></p>
        </div>
    <%end%>
<%end%>

和型号:

class Senior < ActiveRecord::Base

  mount_uploader :image, ImageUploader
  #attr_accessible :image
  #attr_accessible :first_name, :last_name, :street1, :street2, :city, :state, :zipcode, :country, :phone, :image, :employer_id, :phin
  attr_protected :nil

  has_many :senior_users
  has_many :short_forms
  has_one :long_form

  has_many :users, :as => :workers, :through => :senior_users

  belongs_to :employer


  def long_name
    "#{self.last_name}, #{self.first_name}"
  end
end

和 image_uploader.rb:

# encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
   #include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :scale => [50, 50]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  # def extension_white_list
  #   %w(jpg jpeg gif png)
  # end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end
4

2 回答 2

0

如果有人再次来到这里,我会留下这个答案。尽管通过 ajax 的 rails form_for POST 确实不支持通过 AJAX 发送文件,即使使用 multipart: true,也可以使用 gem“remotipart”来完成。这样,无需回退以提交具有整页刷新的表单。回应@xxyyxx 的回答。

于 2013-09-17T16:02:18.180 回答
0

由于这里的答案解决了问题:Rails file_field does not upload anything

我被带入工作的网站使用 jQuery mobile,默认情况下使用 ajax 提交所有表单,但是不能通过 ajax 提交文件。解决方案是添加

:html => { :'data-ajax' => false }

到表格。

于 2013-08-23T16:04:34.713 回答