Kundan Sinha
Kundan Sinha

Reputation: 23

How to upload image in ruby app folder and insert url in database column

Controller

Save the object

How to Use This Controller to insert image in any folder and image url store in database
def create_json

    @user = User.new(userFirstName: params[:userFirstName], userLastName: params[:userLastName], userEmail: params[:userEmail], password: encrypted_password, userImage: params[:userImage])
if @user.save       #if save succeeds, redirect to the index action
        redirect_to(:action => 'show', id: User.last.id)
        else
    #if not succeeds, redirect to the index action  
        redirect_to(:action => 'new')
        end end

Upvotes: 0

Views: 966

Answers (4)

Kundan Sinha
Kundan Sinha

Reputation: 26

User(Model) mount_uploader :userImage, AvatarUploader

UsersController -> @user = User.new(user_params)
                    if @user.save
                     redirect_to(:action => 'show', id: User.last.id)
                    else
                    render :json => data_hash2, :content_type => 'application/json' 
                    end



class AvatarUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
        "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end
def extension_white_list
        %w(jpg jpeg gif png) 
    end  end

If You Using Web Service

<form action="http://ruby/controllername/create" lass="new_user" id="new_user" enctype="multipart/form-data" accept-charset="UTF-8" method="post">

Upvotes: 1

Timmy Von Heiss
Timmy Von Heiss

Reputation: 2218

Create a column url:string in your model. Add it as a hidden field in your form. Set its value to the same as your remote_url using .js

var urladdy = input.remoteurl.val()

input.url.val(urladdy)

This will upload image with carrier wave and also save its url as string.

Upvotes: 0

Anna S
Anna S

Reputation: 157

I don't know of an approach that can cover both: image uploading and then saving the url on the database.

You can do one or the other:

1) Use a gem called paperclip that helps upload image files.

See this links:

https://github.com/thoughtbot/paperclip#ruby-and-rails

https://teamtreehouse.com/library/image-uploads-in-ruby-on-rails-41 (paperclip, carrierwave, and dragonfly)

I have used paperclip and it has worked well for me.

OR:

2) Include it as an additional field on your filename.html.erb for your image_url

One caveat, of using this though is that this can be changed and you have no control of the image that might show on your site. Thus, the image_url should be from a reputable CDN or at least from a reputable user uploading to your site.

Upvotes: 0

Pradeep Sapkota
Pradeep Sapkota

Reputation: 2082

View following method for uploading files to app/assets/images/people_profile_images folder

First upload Image like this:

@user =User.new(user_params)
  #this will create user form paramaters given
uploaded_file = params[:user][:photo]
#this will add uploaded image or file in uplaoded_fie
if (uploaded_file)
  File.open(Rails.root.join('app/assets', 'images/people_profile_images', uploaded_file.original_filename), 'wb') do |file|
    file.write(uploaded_file.read)
    @user.photo = "/assets/people_profile_images/"+uploaded_file.original_filename
  end
else
  @user.photo = "/assets/people_profile_images/default_avatar.png"
end

then save user

@user.save

Upvotes: 0

Related Questions