2
votes

I'm trying to upload images through carrier wave, and using rails.

using minimagick.

Actually, the uploads working properly I think, but images are not shown up. Also it shows nil value in database.

here's my code.

  1. app/models/user.rb

class User < ActiveRecord::Base

mount_uploader :image, ImageUploader

end

  1. mypage.html.erb (form action to "edit_complete")

<input type="file" name='image'>

  1. users_controller.rb

def edit_complete

user = User.find(session[:user_id])

user.image = params[:image]

user.save

redirect_to :back

end

  1. app/uploaders/image_uploader.rb

class ImageUploader < CarrierWave::Uploader::Base

include CarrierWave::MiniMagick

storage :file

def store_dir

"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"

end

end

No errors appeared, but nothing saved in database.

I expect image files should be saved in public/upload folder, and image file should be shown in database in rails c User.all.

However, when I upload 1.png file, there are nothing saved, and public/upload folder is not generated. Also in database shows nil value.

Is there any solution for this?

2

2 Answers

2
votes

In file upload You need to use multipart option in form tag ie multipart option should be true in form tag.

1
votes

Try something like this:

   <%= form_for @user, :html => {:multipart => true} do |f| %>
        <%= f.file_field :image%>
        <%= f.submit 'Edit'%>
    <% end %>