0

I am very new to javascript, html, and haml, so the answer is painfully hopefully obvious for some of you...

Forgive me for my poor understanding of these languages.

I am trying to call a javascript function inside an if statement (the whole script is written in haml). Once it evaluates to 1, aka true, it will then execute code in a do loop. Here is the code:

.container
  %section#admin-order-list
    %table.table
      %thead
        %tr
           %th Client Name
           -# %th Client Email
           %th Client Phone
          %th Expert Name
          -# %th Expert Email
          %th Expert Phone
          -# %th Duration
          %th Date Requested
          %th Conference Number
      %tbody
        - @orders.each do |order|
          -if (check_valid(order.requester_id, order.user_id) == 1) 
            - requester = User.find(order.requester_id)
            - my_expert = User.find(order.user_id)
            %tr
              %td
                = requester.first_name
                = requester.last_name
              -# %td= requester.email
              %td
                = requester.phone_number
              %td
                = my_expert.first_name
                = my_expert.first_name
                = my_expert.last_name
              -# %td= order.user.email
              %td
                = my_expert.phone_number
              -# %td= order.duration
              %td
                = format_date(order.created_at)
              %td
                = order.conference_number

:javascript
  function check_valid(the_requester, the_expert){
    if (typeof the_requester !== 'undefined' && typeof the_expert !== 'undefined') {
      return 1;
    }
    else {
      return 0;
    }
  }
BenjiRoo
  • 3
  • 4
  • Possible duplicate of [Include inline JS in HAML](http://stackoverflow.com/questions/9744169/include-inline-js-in-haml) – imtheman Jul 07 '16 at 21:02

1 Answers1

0

You are mixing server side and client side execution. You're trying to call your javascript check_valid function (client-side) from Rails (server-side). That will not work.

Just as a learning exercise, reimplement your business logic in a Rails helper that you can code exactly as you're doing, and get rid of that javascript.

# app/helpers/application_helper.rb
module ApplicationHelper
  def check_valid(one_id, other_id)
    one_id && other_id
  end
end

And then consider refactoring all that code completely, which is ill-designed, and load those requester and my_expert into instance Ruby variables in your controller, reimplementing your failsafe logic checking presence for those variables.

dgilperez
  • 10,716
  • 8
  • 68
  • 96
  • 1
    Thank you! I am very new to all of this so this tip is incredibly helpful in just learning how to piece things together... – BenjiRoo Jul 25 '16 at 17:50