0

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

4 Answers 4

1

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">
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.