Rails Wicked Gem - Odd Routing Issue

124 Views Asked by At

I have been banging my head against the wall all day today with this problem. I am building an onboarding wizard where I collect business information. It's many steps, but for this I will post simply the 2 step process.

In testing, I can get the wizard/routing/POST to the database correct for my @user model, but when I try to use a newly created database model: onboarding, I run into the following error

ActionController::RoutingError (No route matches [POST] "/onboarding/business_details")

This process works when I using the @user model to insert data (hitting the same /onboarding/business_details endpoint that fails when I use @onboarding model)

#app/controller/onboarding_controller

class AfterSignupController < ApplicationController
  before_action :authenticate_user!
  skip_before_action :verify_user_steps!
  include Wicked::Wizard

  steps :lets_get_started, :business_details

  def show
    @user = current_user
    case step
    when :terms
    when :privacy
    when :user_information
    end

    render_wizard
  end

  def update
    @user = current_user
    @user.update(user_params)
    render_wizard @user
  end

  private

    def redirect_to_finish_wizard(options, params)
      redirect_to root_path, notice: 'Thank you for signing up.'
    end

    def user_params
      params.require(:user).permit(:first_name, :last_name)
    end
end

#app/views/onboarding/business_details.html.erb
<h1>User Information</h1>

<%= form_with model: @user, url: wizard_path do |f| %>
  <div>
    First Name: <%= f.text_field :first_name %>
  </div>
  
  <div>
    Last Name: <%= f.text_field :last_name %>
  </div>
  
  <div>
    <%= f.submit %>
  </div>
<% end %>

#config/routes

resources :onboarding, only: %i[show update create]

Now, lets say I swap out @user and use @onboarding. This process does not work when trying to create for my @onboarding table

#config/routes

resources :onboarding, only: %i[show update create]
#app/views/onboarding/business_details.html.erb

<h1>User Information</h1>

<%= form_with model: @onboarding, url: wizard_path do |f| %>
  <div>
    Legal Name: <%= f.text_field :legal_name %>
  </div>
  
  <div>
    DBA: <%= f.text_field :dba %>
  </div>
  
  <div>
    <%= f.submit %>
  </div>
<% end %>

#note: If I use <%= form_with model: @onboarding, url: onboarding_index_path do |f| %> I do not receive the routing error. However the data does not insert into the database. I'd rather use wizard_path anyways

#app/controller/onboarding_controller

class OnboardingController < ApplicationController
  before_action :authenticate_user!
  skip_before_action :verify_onboarding_steps!
  include Wicked::Wizard

  steps :lets_get_started, :business_details

  def show
    @user = current_user
    case step
    when :lets_get_started
    when :business_details
    end

    render_wizard
  end

  def update
    @onboarding = Onboarding.find(params[:onboarding_id])
    @onboarding.update(onboading_params)
    render_wizard @onboarding
  end

  def create
    @onboarding = Onboarding.new(onboading_params)
    if @onboarding.save
      render_wizard @onboarding
    else
      render :new
    end
  end

  private

  def redirect_to_finish_wizard(_options, _params)
    # change root path to: application pending screen/path
    redirect_to root_path, notice: 'Thank you for signing up.'
  end

  def onboading_params
    params.require(:onboarding).permit(:legal_name, :dba)
  end
end
schema.rb

create_table "onboardings", force: :cascade do |t|
    t.string "legal_name"
    t.string "dba"
end

Any and all help would be greatly appreciate. I have been struggling with this big time. Thanks.

1

There are 1 best solutions below

0
On

Ended up building my own wizard from scratch