ruby-on-railsrubyrubygemsruby-on-rails-6aasm

acts_as_state_machine helper method rails 6


i have a verified and unverified states in my booking model, how do i implement helper methods for my views? i would like something like this in my index views.

  <% @bookings.each do |booking| %>
    <%= link_to booking_path(booking) do %>
        <%= booking.name %>
        <% if verified_booking %>/* here is where i want implemented*/
            <div class="pt-4 font-semibold"><i class="fa fa-user-check"></i></div>
        <% end %     
    <% end %>
    </div>
  <% end %>

helper method

  def verified_booking
    !!Booking.verified
  end

booking model

  include AASM
  aasm :column => :state, :whiny_transitions => false do
    state :unverified, initial: true
    state :verified
    event :verify do
      transitions from: [:unverified], to: :verified
    end
  end

Solution

  • ApplicationHelper.rb

    def verified_booking(state)
     # check state in the database if it is verified
     # return true if state is verified
     # return false if not verified
    end
    

    html.erb

    <% @bookings.each do |booking| %>
        <%= link_to booking_path(booking) do %>
            <%= booking.name %>
            <% if verified_booking(booking.state) %>/* here is where i want implemented*/
                <div class="pt-4 font-semibold"><i class="fa fa-user-check"></i></div>
            <% end %     
        <% end %>
        </div>
      <% end %>