How print ruby var inside haml in js area? - ruby-on-rails

I am beginner in rails.
I have view that i try to print inside him #myvar inside js alert.
%script(type="text/javascript")
alert(#myvar);
But nothing happens,
What should I change?

First of all, you should be using :javascript filter instead of %script definition. In javascript filter you are able to use string interpolation. Try this:
:javascript
alert(#{#myvar});

Related

Thymeleaf - use javascript as th:if condition

I am trying to use a javascript condition as a 'th:if' condition.
In specific i want to show a html element only when a scrollbar is existing in another element and tryed like this:
<div th:if="'javascript:document.getElementById(\'my-element\').scrollHeight>document.getElementById(\'my-element\').clientHeight'"></div>
Is something like this posible? Or should I do this in the '$(document).ready' function?
As #andrewjames rightly commented above, you cant embed evaluated JavaScript expressions into Thymeleaf. You might want to change your approach.
Try something like this to use javascript and thymeleaf together -
<script th:inline="javascript">
...
//your code
...
</script>
Read more about how to use thymeleaf and javascript together in official documentation -
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#javascript-inlining

Proper syntax for including a ruby variable in an Ajax call inside a SLIM view

Rails
Ajax
SLIM
I would like to use SLIM for my Rails views, but I can't figure out the syntax for using a Ruby variable inside an Ajax call.
If my view has an html.erb extension, I can put in it something like this:
<script>
$('#MyDivId').replaceWith({
ajax: '/users_activities?email=<%= #user.email%>'
});
</script>
But, if this was a .html.slim view, if I do:
javascript:
$('#MyDivId').replaceWith({
ajax: '/users_activities?email=<%= #user.email%>'
});
I would get an error message. Any idea what the proper syntax is for doing this in SLIM?
Use this:
'/users_activities?email=#{#user.email}'

Using render_async to call a partial with highcharts

I am using Ruby on Rails and trying to call a partial named _bmetric by using render_async in the index method. This partial has html code and also a highcharts (js). The problem is that the highcharts does not show up. However, if I use render instead of rander_async, there is no problem.
Can someone please tell me what should I consider while using render_async to call a partial with highcharts?
Check out this answer https://stackoverflow.com/a/52842726/4158895
It should help you with your problem.
When you try to render a nested partial, render_async loads JS code inside a script tag that needs to be evaluated to do another request. jQuery has a powerfull method .replaceWith(text) which evaluates any script tags in the text variable. This is not so easy to achieve in plain JS. I'm trying to come up with a solution for this.

Call a Ruby on Rails helper method from a JavaScript js.erb file

Is it possible to call a ruby helper method from within a js.erb file?
I have a helper in application_helper.rb called solve which makes a API call to a third party service, and they only have a ruby client.
The js.erb file isn't being run on the client side, it is being called from within a controller method and run server side as it is PhantomJS.
I call the JS file from the controller
Phantomjs.run('phantom.js.erb', url)
Then within the PhantomJS js.erb file I have tried
var response = '<%= solve(variable) %>'
which just sets the variable as the string <%= solve(variable) %>
I have also tried
var response = <%= solve(variable) %>
but that just seems to make the application hang and become unresponsive.
I have seen other questions similar to this. In those questions they are asking if it is possible to call it from client side JS which I know you need to use an ajax request to do so.
Is this possible?
Try this:
var content = '#{solve()}'
Need a bit more context for this question, but I'll try my best to answer:
Essentially, you wouldn't be able to access your application_helper methods outside of any .erb files. (ie. if you have application.js or any other js file in your pipeline and you are trying to <%= solve %> from there it wouldn't work - mainly because it isn't an .erb file)
There are a lot of ways and architecture to go about solving this, but here are two simple ones:
If you put the JS you want to evaluate inline on the same page as your partial/html.erb page by using <script> //JS ERB CODE GOES HERE </script> It will actually evaluate properly since it is inside of an erb file. However, this is generally looked upon as unclean...
What you probably want to do is pass the value (presumably) you want that comes from the "solve" application_helper in a 'data' attribute on the html element that it affects. By utilizing "unobtrusive javascript" in this way, you simply pass the value through markup and then in your JS you can get the variable by using jQuery code like this. Here's an example:
<%= link_to "test_link", root_path, :data => {:solve => solve } %>
Of course it doesn't have to be a link, any HTML element will do, and then in your jQuery:
$("#test_link").data("solve");
will return to you whatever output comes out of your "solve" method in the application_helper.
it can possible but there are different ways to do it. one way is define the method in helper_method in your controller and call it from your view. and another way is use the gon gem to access some controller value in your javascript. please check what is best for you please check the below links for more help
http://apidock.com/rails/ActionController/Helpers/ClassMethods/helper_method
https://github.com/gazay/gon
http://railscasts.com/episodes/324-passing-data-to-javascript

How do you write multiline if/else statements with inline javascript HAML?

I'm basically trying to produce jQuery effects based on the data I am coming in with from the server. I've tried multiple methods but its not coming out correctly
:javascript
"#{if #user.nil?}"
$('#test-container').show();
"#{end}"
The contents of #{...} need to be a single expression, and is allowed to go over multiple lines. Inside #{...} you can use literal strings simply by quoting them, and you don’t need quotes around the whole thing.
:javascript
#{if #user.nil?
"$('#test-container').show();"
end}
In this case you can do it as a single line:
:javascript
#{"$('#test-container').show();" if #user.nil?}
In general you want to avoid complex multiline interpolated blocks like in the first example. If necessary you should look at creating helpers to keep your views simple and understandable.
Does this work?
- unless #user
:javascript
$('#test-container').show();

Resources