Interpolate ruby if statement in haml javascript block - javascript

This is the code I currently have:
:javascript
// do something
- if current_user.role? :client
:javascript
//do something else
It's obviously not very clean since I'm repeating the :javascript haml filter. I'd like to avoid that, but I don't know how to properly write a Ruby "if" statement inside a HAML :javascript block.
I know how to interpolate variables with #{}, but how do you do the same for whole if/for/etc. statements?

I do this:
if(#{params[:param_to_check_for].blank? ? "true" : "false"})
In the above case, the condition is
params[:param_to_check_for].blank?.
It renders the JavaScript as either: if(false) or if(true)

I haven't tried it, but I'd think you could do
:javascript
// do something
- if current_user.role? :client
="//do something else"
(As in the second example here.)
For a very short bit of javascript, you could also try
:javascript
// do something
#{current_user.role? :client ? "//do something else" : ""}

I've interpolated the Ruby/HAML variable into explicit JS true or false for JS condition like this:
$("#cancel").click( function() {
if (#{ !!my_flag_variable })
window.location.href = "./";
else
$("#dialog").hide();
} );

I've accomplished this via a = yield :foot_scripts call in my layout, then then:
= if condition
- content_for :foot_scripts do
- render :partial => "page_specific_javascript"
in my views — that way I can ensure the Javascript is executed exactly where I want it to be in the DOM (AFTER everything else), and maintain it in a separate file, which is vastly cleaner. Getting script out of your view files? GOOD!

That's pretty much right.
The other option is you could put it in a .js.erb partial and render that from your haml view.
Then you can just do
// do something
<% if condition == true %>
// do something else
<% end %>

Related

Coffesscript variables in ruby code pieces

I'm just trying to call rails helper from my coffeescript.
The problem is that coffee variables are unavailable in ruby code pieces that are in <%= ... %> blocks. And if they are processed with #{} it translates to JS in a wrong way.
So, let me illustrate. This is a piece of .js.coffee.erb file:
<% environment.context_class.instance_eval { include InputsHelper } %>
$('#input_input_type').change ->
t = $('#input_input_type').val()
$('.input_address .help-block').html('<%= input_type_hint(t) %>')
This code produces such error: undefined local variable or method 't' for #<#<Class:0x007f5e75ebd860>:0x007f5e785d1410>
Ok, let's put 't' into #{}:
<% environment.context_class.instance_eval { include InputsHelper } %>
$('#input_input_type').change ->
t = $('#input_input_type').val()
$('.input_address .help-block').html("<%= input_type_hint(#{t}) %>")
This translates to JS in a wrong way. The last string looks like this:
$('.input_address .help-block').html("<%= input_type_hint(" + t + ") %>");
Quotes are broken, so it causes rails to fall with the error:
syntax error, unexpected ';', expecting ')' ; _erbout.force_encoding(__ENCODING__) ^
In that way I don't know how to manage with this. Is there any way to do it?
There is no easy way to do this.
The problem is that the coffee script executes in the browser, but the stuff in the <%= ... %> executes on the server while the server builds the coffeescript.
So when you say input_type_hint(t) this is running on the server, but the poor sad server doesn't have a clue what "t" is.
To make this work you are going to have recode the entire input_type_hint method in coffeescript, so that it can run in the browser too! (and then it won't be in the <%= => thingy.
By the way you might want to check out http://ruby-hyperloop.io as instead of using coffeescript you just write ruby everywhere, and you basically don't need this clumsy ERB business.

Rails - how put javascript variable into erb code

I've seen some similar question, but still don't know how achieve my goal.
I want to do that thing:
Display modal with products represented by divs.
User choose some products (I add .active class to chosen products)
Then I use jQuery to make array of chosen products ids.
In the end I want to create div for each product, which will be include some informations about this product from database. I'm using js .append() to do it. And here is a problem.
This code is in my script tag in proper view.
var chosen_products_array;
$('.product-to-choose').click(function() {
$(this).toggleClass("active");
$(this).find('i').toggleClass("visible");
});
$('#chosen-products-confirm').click(function() {
var chosen_products = $('.product-to-choose.active');
chosen_products_array = jQuery.makeArray( chosen_products );
});
$('#confirm').click(function() {
var textToInsert = '<div><h4>Products:</h4>';
$.each(chosen_products_array, function(count, item) {
var id = $(item).attr('id').substr(8); // id is product_number
textToInsert += '<div><li name="chosen-product"> <%= current_user.products.find(' + id +').name %></li></div>';
});
textToInsert += '</div>';
$('#div-example').append(textToInsert);
});
My controller create action:
def create
#meal = current_user.meals.build(meal_params)
if #meal.save
current_user.type_tags.each do |type_tag|
key = "type#{type_tag.id}"
if params[key]
#meal.type_tags << type_tag # zapis do bazy danych
end
end
flash[:success] = "Create new meal!"
redirect_to meals_path
else
#render 'meals/new'
end
end
This line generates error:
textToInsert += '<div><li name="chosen-product"> <%= current_user.products.find(' + id +').name %></li></div>';
Error:
Couldn't find Product with 'id'=+id+ [WHERE "products"."user_id" = ?]
I know there is a problem in passing js variables to rails erb, but how can I solve it in this case? I tried with gon too.
In my controller, I added:
gon.products = current_user.products
and then try
alert(gon.products)
it's ok - array of objects, but when I try
alert(gon.products.find(1))
it doesn't work.
Have you got any idea how can I put this javascript id variable to erb code? Or maybe is any different solution?
Probably the best would be to add a remote form (it may be hidden) that posts whatever you want (javascript values) and then it is handled by a controller. Then you can render it with js views.
This would probably be the most "rails-like" solution.
Here's some code:
In the routes:
resource :my_form, only: :create
In the view:
<%= form_tag(my_form_path, remote: true, method: :post) do %>
<%= hidden_field_tag :val %>
<% end %>
You alter the val dynamically by javascript, and submit the form whenever you want to.
Then, in the controller you have something like this:
respond_to :js
def create
# do something with params['val']
#my_val = params['val']
end
And you would also add create.js.erb view, where you could add some javascript that can use any of the instance variables created in the controller.
//create.js.erb
$('#someElement').html('<%= #my_val %>');
Let me stress if it is not clear, the form is remote (ajax), so it is sent in the background and the page is not reloaded.
When the browser receives the response, it may contain any javascript you decide to render (the contents of create.js.erb), and then the browser executes that javascript.
So, you can add there any jquery commands with arguments that you changed dynamically by rails, or you could also render html partials and replace any elements with them, like this:
//create.js.erb
$('#my_id').html('<%= j(render partial: 'my_form/_some_partial.html.erb', locals: { my_val: #my_val }) %>');
More details on Working with JavaScript in Rails
UPDATE
Another, inferior but simpler to implement solution would be to always render all products, but have them hidden initially, and then show them by jQuery on demand. You would just need to add appropriate css classes or data attributes to those products, as well as the links you use to select them.
The problem is that erb code is evaluated on the server before the browser even fetches the javascript file, so it doesn't make sense to use this way. It's recommended that you only use erb in your javascript for simple things like asset_path
I think first make your model visible to the browser as js variable(json object) . Some thing like below at the end of your view file(below is the haml syntax):
:javascript
var userProducts = #{#current_user.products.to_json};
Then use it in javascript expression

Passing a local variable into another view's JS - Rails

Is there a way to grab / fetch a variable inside js between two different views? Say I have an index view with does:
<% foo.each do |foo| %>
<%= link_to 'bar', bar_path %>
<% end %>
Then in bar I want to add some jQuery to a specific element with foo.id selector:
$('div#<%= foo.id %>').fadeOut(); // I know the interpolation wouldn't work here
I know I can pass foo.id to bar with my path or send it via the locals hash, but how would I "grab" it with jQuery?
You can easily pass variables to JS using a gem called gon.
gem "gon"
All you have to do is add this gem to you Gemfile and then you can call gon inside any controller like this:
class PaymentsController < ApplicationController
def new
...
gon.controller = "payments"
gon.action = "new"
gon.message = "Hello World!"
...
end
end
And now, at any javascript (in following case, coffeescript) you can:
$ ->
# Check if it is the target controller and action
if gon? && gon.controller is "payments" && gon.action is "new"
alert gon.message
Is there a way to grab / fetch a variable inside js between two
different views
Not without hacking.
JS is an client-side technology, meaning it loads in the browser, not on the server. This is famous for causing so many issues for developers trying to bind to dynamically-named objects, much like what you're trying to do.
There are several ways around this. I'll go through them for you:
Class
The first way is to use a broader identifier, such as element type or class.
You're currently trying to select foo.id, which means you're going to have to pass that data directly to JS somehow. Inefficient.
A much better way would be to use something like this:
$("a.bar").on("click", function(e){
$(this).fadeOut();
});
--
URL
According to this resource, there are no inbuilt ways to pull query strings from JS: How to get the value from the GET parameters?
You may be able to do something like this:
$(document).ready(function(){
foo = window.location.search.substring(1); //Considering you have http://url.com/your/foo/id
$("#" + foo).fadeOut();
});
--
ERB
There is a final alternative, although this only works if you don't precompile your JS (which I'm not sure is actually feasible any more). That is to use ERB directly in your JS, rather like what you've done in the question.
Whilst this works for dynamic references (such as paths), I don't think it can pull data from a controller. Unless, of course, you put the script directly in your view - which goes against the conventions of MVC.
#app/views/bar/show.html.erb
<script>
// your code here
</script>
I think you need to create a js.erb file and in that pass a variable defined in your controller action.
The code insde js.erb would look like this :
<% fields_html = render "fields", :entity => #model %>
$("#fields-list").html("<%= escape_javascript fields_html %>")
Here fields is a partial I have created. :entity is basically passing of the variable value. #model is the variable defined inside controller action. fields-list is the id of the field I want to load using jquery.

Rails ERB Inside Javascript

I'm trying to use some View instance variables inside Javascript.
My JS file is included in the head tag of my HTML. And it's a *.js file in assets/javascript.
When I try to...
console.log(<%= #some_instance_variable %>)
... I get syntax error. And if I try to...
console.log('<%= #some_instance_variable %>')
... the output is just the string <%= #some_instance_variable %>. How can I do to obtain the instance variable's actual value?. I'd really appreciate any help!.
Regards!.
You can include it with:
<%= javascript_include_tag('file_name.js.erb'.sub('.erb', '')) %>
But in fact it's a very ugly solution. If you want to use rails variables in JS scripts, use gon gem.

pass a parameter from a views to a javascript

I have the next variable in my main_controller.rb:
def create:
#token = "24vgd32"
end
in my views/create.erb, I wrote:
<%= javascript_tag do %>
window.my_token = '<%= j #token %>';
<% end %>
by this way, I can use this variable in any of the JavaScript files that this page references.
but how can I use it in my application.js?
this is my application.js:
$(document).ready(function(){
licensario.getLicense({
wizardToken: my_token
});
});
any help appreciated!
In your create.html.erb use this
<%= hidden_field_tag :my_token, #token, :id => 'some_field_id' %>
replace the some_field_id and my_token with desired names.
In your javascript file
$(document).ready(function(){
token = $('#some_field_id').val();
//use your token for any other means.
});
I see 3 ways :
You can use erb in your js file, naming it application.js.erb. However, the data you want to pass must be available when the file is compiled, so this is probably not what you want to do;
You can make a XHR request to your server to fetch the value you want;
You can embed the value in the dom of your view. Some will suggest using a hidden field, I rather tend to use data attributes. For instance, <body data-my-value="32">.... You can retrieve it in your js by doing $('body').data('my-value'). Be careful to call this once the dom is loaded.
I tend to use the latter.
window.my_token makes my_token a variable on the global scope; once it's been defined it is available to application.js as simply my_token.

Categories