Haml and JavaScript and Ruby communication

291 views Asked by At

I have some code, like this:

  %div{:id=>"alerts_tab",:class=>"settings_tab"}
    %div{:class=>"main_block"}
      %div{:class=>"left_block"}
        %p Select 1
        = radio_button_tag 'gender', 'male', :id=>"daily_frequency"
        Daily
        = radio_button_tag 'gender', 'male', :id=>"weekly_frequency"
        Weekly
        = radio_button_tag 'gender', 'male', :id=>"monthly_frequency"
        Monthly
        %p Try it
        %input{:name => "malfunction_affects_more", :type => "checkbox", :class => "check_box", :value=>"off"}
        AAAAAAAAAAAAAAAAAAA
        %select{:class=>"custom_drop_down"}
          %option 1% users 'test'

I trying use JS for select data and move findings to ruby code. But I have some problems: 1. If I write JS code

:javascript
  $( "#save_settings" ).click(function() {
    var value = document.getElementById('daily_frequency');
    alert(value);
  });

It is not working, why?

  1. If I catch all data how to move findings to ruby code. I don't know.

Please, help me with this problems.

2

There are 2 answers

4
Piotr Kruczek On BEST ANSWER

Wrong syntax. Check this out

The 3rd parameter sets the checked attribute, so this should work:

= radio_button_tag 'gender', 'male', false, :id=>"daily_frequency"

Side note for useful haml syntax:

%div{:id=>"alerts_tab",:class=>"settings_tab"}

can be written simply as

#alerts_tab.settings_tab
0
Max Williams On

Your syntax is wrong: it should be

var value = document.getElementById('daily_frequency').value;

however, since you're using jQuery, you might as well use jQuery's syntax, which is neater:

var value = $("#daily_frequency").val();