0

In my application_controller.

def ensure_service_agreement
  if current_user.agrees_to_service == true
    @tos = true
  else
    @tos = false
  end
end

I have the following code in my application.js file.

var tos = "<%= @tos %>";
if(tos == false){
  $(".new-session-home").on('click', function (){
      $('.booking.modal')
      .modal('show');
    })
}else{
  $(".new-session-home").on('click', function (){
      $('.tos.modal')
      .modal('show');
    })
}

The javascript doesn't seem to register the var tos. When I set the first if statement to either true or false, I get the second modal (the tos modal)

If I set tos = 4 and then check for that number, it works as it should so I know the problem is with the rails instance variable.

How can I get the JS to understand my instance variable?

3
  • 1
    it js file, it can't read erb syntax.. Commented Dec 8, 2014 at 6:46
  • That's my problem, I need coffeescript then? Commented Dec 8, 2014 at 6:49
  • noeps, you need controller_action_name.js.erb Commented Dec 8, 2014 at 6:59

3 Answers 3

1

I used the gon gem.

Steps:

gemfile.rb

gem 'gon'

application.html.erb

<%= include_gon %>

back to my application_controller.rb

def ensure_service_agreement
 if current_user.agrees_to_service == true
  @tos = true
  gon.tos = true
 else
  @tos = false
  gon.tos = false
 end
end

and finally my js

dayClick: function(date, allDay, jsEvent, view) {
    if(gon.tos == true) {
              $('.booking.modal').modal('show');
              console.log(date)
    } else {
              $('.tos.modal').modal('show');
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are trying to manipulate rails code in js file. But you will not get any erb syntax there.

There are multiple solution to solve this kind of request. If you are submitting form or redirecting to some url, Then you can use ujs to do this (Using :remote => true).

UJS respond in .js.erb file there you can access both js and erb syntax.

If you are not submitting or there is no url change then you can easily do this with jquery only. like

<a href='#' onclick="somefunction(true/false)">booking/tos</a> 

As your code structure you should go for UJS.

3 Comments

I need to manipulate a callback based on a rails boolean. It's loading different modals, no URI changes.
You can use UJS for this. You must be on some controller which is associated to this callback. someaction.js.erb
0

Not sure, this is an ideal solution.

You have set the value in ensure_service_agreement method.

Use renderer file like ensure_service_agreement.js.erb or ensure_service_agreement.html.erb

and keep the value in input tag.

<input name='' id='tos' value = '<%= @tos %>'> use type hidden or style with display:none

and access in Js file like

var tos = $('#tos').val();

1 Comment

I'm trying to manipulate a callback from a jquery library based on my boolean, I'm not submitting a form. There is no input tag.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.