[RUBY] [rails6.0.0] How to save images using Active Storage in wizard format


Create a form in a wizard format when registering and profile. I wanted to save the image in my profile, but it didn't work, so I wrote it as a memorandum.

What is the wizard format? For those who say Please refer to here


Development environment

MacOS Catalina 10.15.6 Rails 6.0.0 Ruby 2.6.5

Table structure

I'm trying to register as a user using devise. Only nickname is added to the user table. The profile table includes a twitter link. I want to save an image with active_strage in the profile table.

Implemented wizard format

Regarding the implementation of these functions This article is quite close, so I referred to it.


In the wizard format, I was able to save everything except the image, but the image is not saved.


class Users::RegistrationsController < Devise::RegistrationsController

  def create
    @user = User.new(sign_up_params)
    unless @user.valid?
      render :new and return
    session["devise.regist_data"] = {user: @user.attributes}
    session["devise.regist_data"][:user]["password"] = params[:user][:password]
    @profile = @user.build_profile
    render :new_profile

  def create_profile
    @user = User.new(session["devise.regist_data"]["user"])
    @profile = Profile.new(profile_params)
    unless @profile.valid?
      render :new_profile
    sign_in(:user, @user)
    redirect_to root_path


  def profile_params
    params.require(:profile).permit(:avatar, :favorite_beer, :twitter_link, :info)

It seems that the following description in this was bad.



Here, @user and @profile are associated with build and saved together with @ user.save, but it seems that it is not working well due to saving here. Find this article

Cause The timing when the file is actually saved in Active Storage is when the model is saved and the process is committed, so even if you attach an image from the model before it is saved, the file seems to be in an incomplete state.

It seems that Active Strage can not go unless you save the model properly, so I solved it by changing the description as follows.


#Saved in each model
# @user.build_profile(@profile.attributes)
@profile.user_id = @user.id


Honestly, the cause of the error may be that I used it even though I didn't understand how to save using build. I hope you find it helpful.

Recommended Posts

[rails6.0.0] How to save images using Active Storage in wizard format
How to link images using FactoryBot Active Storage
[Rails 6] Add images to seed files (using Active Storage)
[Rails] How to use Active Storage
How to implement image posting function using Active Storage in Ruby on Rails
[Rails] How to upload images using Carrierwave
[Rails] How to upload multiple images using Carrierwave
[rails] How to post images
[Rails] Save images using carrierwave
[Rails API + Vue] Upload and display images using Active Storage
How to deploy jQuery in your Rails app using Webpacker
[Rails] How to delete images uploaded by carrierwave (using devise)
[Rails] How to write in Japanese
How to introduce jQuery in Rails 6
How to install Swiper in Rails
[Rails] Implementation of new registration function in wizard format using devise
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws
[Rails] How to upload images to AWS S3 using refile and refile-s3
How to implement search functionality in Rails
How to change app name in rails
How to use custom helpers in rails
How to insert a video in Rails
How to use MySQL in Rails tutorial
[rails] How to configure routing in resources
Rails Active Storage shrinks images before uploading
How to implement ranking functionality in Rails
How to implement image posting using rails
How to use credentials.yml.enc introduced in Rails 5.2
[Rails] How to handle data using enum
How to implement a slideshow using slick in Rails (one by one & multiple by one)
How to create a query using variables in GraphQL [Using Ruby on Rails]
[Rails] How to create a graph using lazy_high_charts
[Rails] How to use select boxes in Ransack
How to translate Rails into Japanese in general
How to prevent direct URL typing in Rails
How to save images on Heroku to S3 on AWS
How to separate .scss by controller in Rails
How to conditionally add html.erb class in Rails
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to use JQuery in js.erb of Rails6
[Ruby on Rails] How to install Bootstrap in Rails
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
Create a drag-and-drop markdown editor in Rails 6 (using Active Storage, SimpleMDE and Inline Attachment)
How to set the display time to Japan time in Rails
How to implement guest login in 5 minutes in rails portfolio
How to implement a like feature in Ajax in Rails
[Ruby on Rails] How to write enum in Japanese
Use images in Rails
[rails] How to post images
[Rails] Upload videos with Rails (ActiveStorage)
[Rails] Save images using carrierwave
Rails6 OmniAuth activestorage Get user image
[How to insert a video in haml with Rails]
How to uninstall Rails
How to delete untagged images in bulk with Docker
How to write a date comparison search in Rails
Use images in Rails
How to convert A to a and a to A using AND and OR in Java
How to download images from AWS S3 (rails, carrierwave)
How to query Array in jsonb with Rails + postgres
[Rails 6] How to set a background image in Rails [CSS]
Rails learning How to implement search function using ActiveModel
[Rails] How to load JavaScript in a specific view