vendredi 16 février 2018

How do I use my accordion's checkboxes within my order wizard?

I've posted my controller, model, form view & partial view for the checkbox step code I'm struggling with below.

Up until now, I had been using radio buttons for selecting 1 specific value out of multiple options during each step of the wizard. It's worked perfectly and saves to the database correctly how I'd like.

However, for the last step in the wizard, I need to choose multiple values out of the options and have resorted to check_boxes for this.

My problem is, I'm not sure how to correctly save the multiple check_box values to my current model, how to express this in my views correctly and/or if creating a new model for this specific attribute, how to hook it up with how the code is currently set up for the wizard.

I'm currently using the accordion headers as categories to open up the check_box options that will end up saving all together.

IE; 1st guide__choice opens up the panel showing multiple check_box values, 2nd guide__choice opens up a different panel showing it's own category of check_box values, but the Order model needs to know about both the 1st and 2nd guide__choice check_box values.

To me, it sounds like I need a new model or 2 where I can save each guide__choice as a category with the check_boxes values belonging to the category, but I'm not sure how I would hook this all up to the order wizard to go along with the other model. My best guess is having a has_many :through relationship set up between the Order -> newModel :through newModel2

Writing this all out is making me start to think I'll have to/should have a new model for all the attributes i'm using for the Order. But wouldn't that also start calling the database way more often than is necessary?

All and any help is much appreciated, thank you!~

#### MY CONTROLLER ####
class OrdersController < ApplicationController
  def new
    session[:order_params] ||= {}
    @order = Order.new(session[:order_params])
    @order.current_step = session[:order_step]
  end

  def create
    session[:order_params].deep_merge!(params[:order]) if params[:order]
    @order = Order.new(session[:order_params])
    @order.current_step = session[:order_step]
    if @order.valid?
      if params[:back_button]
        @order.previous_step
      elsif @order.last_step?
        @order.save
      else
        @order.next_step
      end
      session[:order_step] = @order.current_step
    end
    if @order.new_record?
      render "new"
    else
      session[:order_step] = session[:order_params] = nil
      flash[:notice] = "Order Saved!"
      redirect_to @order
    end
  end
end

#### MY MODEL ####
class Order < ApplicationRecord
  attr_writer :current_step
  validates_presence_of :attr_one, :if => Proc.new { |o| o.current_step == "step-1" }
  validates_presence_of :attr_two, :if => Proc.new { |o| o.current_step == "step-2" }
  validates_presence_of :attr_three, :if => Proc.new { |o| o.current_step == "step-3" }
  validates_presence_of :attr_four, :if => Proc.new { |o| o.current_step == "step-4" }

  def current_step
    @current_step || steps.first
  end

  def steps
    %w[step-1 step-2 step-3 step-4]
  end

  def next_step
    self.current_step = steps[steps.index(current_step)+1]
  end

  def previous_step
    self.current_step = steps[steps.index(current_step)-1]
  end

  def first_step?
    current_step == steps.first
  end

  def last_step?
    current_step == steps.last
  end
end

#### MY FORM VIEW ####
##### ...ommitting if modelObject.errors.any? block #####
<section class="guide"%>
  <%= render "orders/partials#{order.current_step}-step", :f => f %>
  <div class="actions">
    <div class="guide__next-step">
      <%= button_tag(type: 'submit') do %>
        <h1>Next step</h1>
      <% end %>
    </div>
    <%= f.submit "Back", :name => "back_button" unless order.first_step? %>
  </div>
</section>

#### THE STEP PARTIAL CHECKBOX VIEW I NEED HELP WITH ####
<div class="accordion guide__choices">
  <h3 class="guide__choice">Header 1 drops down checkbox 1 panel</h3>
  <div class="accordion__panel">
    <%= f.check_box :attr_four, {}, "choiceOne" %>
    <%= f.label :attr_four, "Choice One", :value => "choiceOne" %>
  </div>

  <h3 class="guide__choice">Header 2 drops down checkbox 2 panel</h3>
  <div class="accordion__panel">
    <%= f.check_box :attr_four, {}, "choiceTwo" %>
    <%= f.label :attr_four, "Choice Two", :value => "choiceTwo" %>
  </div>

  <h3 class="guide__choice">Header 3 drops down checkbox 3 panel</h3>
  <div class="accordion__panel">
    <%= f.check_box :attr_four, {}, "choiceThree" %>
    <%= f.label :attr_four, ""Choice Three", :value => "choiceThree" %>
  </div>
</div>




Aucun commentaire:

Enregistrer un commentaire