Ruby on Rails和Carrierwave Gem:图片上传Save" Nil"到数据库

时间:2017-01-05 17:28:19

标签: ruby-on-rails ruby paperclip carrierwave

好的,所以我一直试图诊断这几天,而且我把头发拉了出来。出于某种原因,无论我做什么,当我尝试通过Carrierwave上传器上传图像时,它都会节省“nil”。在数据库而不是图像。如果它意味着什么,我也尝试过蜻蜓,我也遇到了问题。任何信息都有助于我真正需要这个固定的。以下是相关文件:

newexpense.html.erb

<%= form_for @new_expenses, html: { multipart: true } do |t| %>
  <fieldset>
    <%= t.text_field :dateActivity, { :id => "expenseDate", :placeholder => "Date of Expense"} %>
    <%= t.text_field :typeOfExpense, { :placeholder => "Type" } %>
    <%= t.text_field :amount, {  :id => "amountInput" , 'placeholder' => 'Amount $0.00', :step => 0.01 } %>
    <%= t.text_field :payment, { :placeholder => "Payment Type" } %>
    <%= t.text_field :notes, {:class => "form-control", :placeholder => "Description"} %>
  </fieldset>
  <div style="line-height:75%;"><br></div>
  <fieldset>
    <%= t.file_field :image %><br>
    <%= button_tag "Submit", type: 'submit', id: "contact-submit" %>
  </fieldset>
<% end %>

<% @expenses.each do |e| %>
  <p>Type: <%= e.typeOfExpense %></p>
  <p>Amount: $<%= e.amount %></p>
  <p>Payment Type: <%= e.payment %></p>
  <p>Description: <%= e.notes %></p>
  <%= image_tag e.image %>
<% end %>

显示除图像之外的所有正确信息。

Expense.rb

class Expense < ActiveRecord::Base
  belongs_to :user
  mount_uploader :image, ImageUploader
  #before_validation {write_attribute(:dateActivity, Date.strptime(@dateActivity, "%m/%d/%Y")) }
end

控制器

    def create
          @new_expenses = Expense.new(params_final_expense)
          if @new_expenses.save
            flash[:success] = "Expense entry created!"

            redirect_to expenses_url
          else
            flash[:success] = "Expense entry failed :("
            redirect_to expenses_url
          end

      end

  private

  def params_final_expense
    new_pages_params_expense.merge(:user => current_user)
  end

  def new_pages_params_expense
    params.require(:expense).permit(:dateActivity, :typeOfExpense, :amount, :payment, :notes, :image)
  end

image_uploader.rb

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


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



end

保存费用时的输出

Processing by PagesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"HUHjhuisdfjwieuhIUHOUHusdf", "expense"=>{"dateActivity"=>"2016-12-19", "typeOfExpense"=>"", "amount"=>"", "payment"=>"", "notes"=>"", "image"=>"043.png"}, "button"=>""}
  User Load (1.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 5  ORDER BY "users"."id" ASC LIMIT 1
   (1.0ms)  begin transaction
  SQL (1.0ms)  INSERT INTO "expenses" ("created_at", "dateActivity", "image", "notes", "payment", "typeOfExpense", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", "2016-12-19 19:56:40.537849"], ["dateActivity", "2016-12-19"], ["image", nil], ["notes", ""], ["payment", ""], ["typeOfExpense", ""], ["updated_at", "2016-12-19 19:56:40.537849"], ["user_id", 5]]

我真的难以理解为什么我的图片不能保存。感谢您提供的任何帮助:D

0 个答案:

没有答案