How to convert rails request.env to javascript? - javascript

I have next js.erb file:
<% if Rails.env == 'production' %>
<% if request.env['SERVER_NAME'] == 'example.com' %>
$(function () {
...
...
});
<% end %>
<% end %>
How to convert second line in JavaScript without Ruby?

Output the SERVER_NAME as a string, and do the comparison client-side:
<% if Rails.env == 'production' %>
if ('<%= request.env['SERVER_NAME'] %>' == 'example.com') {
$(function () {
...
...
});
}
<% end %>
If the second line can "only contain javascript" (I strongly question this) then you can move the Ruby bit out to a variable:
var serverName = '<%= request.env['SERVER_NAME'] %>';
<% if Rails.env == 'production' %>
if (serverName == 'example.com') {
$(function () {
...
...
});
}
<% end %>

I found the answer as:
<% if Rails.env == 'production' %>
if (window.location.hostname == 'example.com') {
$(function () {
...
...
});
}
<% end %>

Related

EJS comparison between two same strings

I try in ejs to compare two Strings which in the current situation are the same and it runs the else case. Here is the code:
<% var idf = notifications.data[i].from; %>
<% var ids = notifications.users[a].id; %>
<% if(idf == ids){ %>
<% index = a; %>
<script>alert('<%= index %>');</script>
<% break; %>
<% }else{ %>
<script>console.log('<%= idf %> - <%= ids %>');</script>
<% } %>
It logs 5d5ecfd1ad6d193de86c2264 - 5d5ecfd1ad6d193de86c2264
Use === instead of == to see if this fixes the issue, as it will prevent internal type coercion.
It might also be worth trimming the strings to see if there is any white space or padding added to them.
You could use the below code to compare two string values in ejs
<% if("some text" === "some text"){ %>
<h1>Yes the above text are equal</h1>
<% } %>
If you are comparing mongoDB objectId, then use toString().
<% var idf = notifications.data[i].from; %>
<% var ids = notifications.users[a].id; %>
<% if(idf.toString() == ids.toString()){ %>
<% index = a; %>
<script>alert('<%= index %>');</script>
<% break; %>
<% }else{ %>
<script>console.log('<%= idf %> - <%= ids %>');</script>
<% } %>

Rails js.erb executing within if statement that is false

animal.js.erb
<% if #animal.type == "Cat" %>
<% if #animal.meow == "Loud" %>
alert("Loud Cat");
<% else %>
alert("Quiet Cat");
<% end %>
<% else %>
<% if #animal.bark == "Ferocious" %>
alert("Ferocious Dog");
<% else %>
alert("Nice Dog");
<% end %>
<% end %>
When #animal.type == "Dog" I stil get an error for #animal.meow method does not exist.
Why is it reading that if statement and erroring?
It appears that .erb is executing the if #animal.meow statement even when the #animal.type is "Dog"
you forgot to add closing quote in line <% if #animal.meow == "Loud %> and in <% if #animal.bark == "Ferocious %>

Changing inline javascript with Perl variables into a function

I currently have a select tag with an inline onChange property like this:
<select id="owner_sel" name="owner"
<% print ' style="display: none"' if(!$user->has_permission($task->cid, $PERMISSION_CHANGE_OWNER)); %>
onChange="if(!this.selectedIndex == 0) {
if(this.form.state_id.value == <%= $STATE_NEW %>) {
this.form.state_id.value = <%= $STATE_ASSIGNED %>
}
} else {
this.form.state_id.value = <%= $STATE_NEW %>
}">
<option></option>
<%
foreach my $h (#sorted_users){
foreach my $uid (keys %{$h}) {
my $selected = ($uid == ($form->{owner} || $task->owner)) ? 'selected="selected"' : '';
print qq{<option value="$uid" $selected>$h->{$uid}</option>};
}
}
%>
</select>
and I wanted to move it into a function like this:
<script>
// code...
</script>
Im pretty newbie with javascript and I also don't know if Perl variables can be called the same way in javascript functions?
edit
<script>
$(document).on('change', '#owner_sel', function() {
if(!this.form.owner_sel.selectedIndex == 0) {
if(this.form.state_id.value == <%= $STATE_NEW %>) {
this.form.state_id.value = <%= $STATE_ASSIGNED %>
}
} else {
this.form.state_id.value = <%= $STATE_NEW %>
}
});
$(document).on('change', '#state_id', function() {
if (this.form.state_id.value = <%= $STATE_NEW %> ){
this.form.owner_sel.selectedIndex = 0;
}
});
</script>
----------Question-----------
I redid my code to something like that. When it is just one function, everything works but when I have both scripts included like the snippet above, there is an error.
I was wondering, in my second function if the state_id = 'NEW' and the owner_sel changes to index '0' does that re-trigger the first function?
<script>
$(document).on('change', '#owner_sel', function() {
if(!this.form.owner_sel.selectedIndex == 0) {
if(this.form.state_id.value == <%= $STATE_NEW %>) {
this.form.state_id.value = <%= $STATE_ASSIGNED %>
}
} else {
this.form.state_id.value = <%= $STATE_NEW %>
}
});
$(document).on('change', '#state_id', function() {
if (this.form.state_id.value == <%= $STATE_NEW %> ){
this.form.owner_sel.selectedIndex = 0;
}
});
</script>
The way you translated it was correct. And yes, Perl variables can be called into the javascript function the same way you called on them inline.
The reason your code is conflicting is because your second line of code in the .on('change', '#state_id' code is using a single '=' and I think you meant to use '==' to check if the state is 'NEW'. The way you have it now would just change the owner_sel.selectedIndex to '0' everytime the state_id is changed.
if (this.form.state_id.value == <%= $STATE_NEW %> ){

jQuery if/else condition doesn't work fine in js.erb files

I'm running into problem with jQuery if/else condition here.
I perform a render partial in both the conditions, when I check by placing a debugger both these conditions get executed irrespective of if condition.
And every-time the partial from else condition is updated even though if condition is true.
<% if #associated_goal.present? %>
<% goal_status = #associated_goal.completion_percentage(#required_tasks) %>
var progress_bar_selector = "#cjs_show_goal_progress_bar_<%=#associated_goal.id.to_s%>";
var progress_from_goals = "#cjs_show_goal_progress_from_goals_<%=#associated_goal.id.to_s%>";
var goal_progress_display = ".cjs-mentoring-model-goal-progress-<%=#associated_goal.id.to_s%>";
if(progress_bar_selector != "")
{
$(progress_bar_selector).html("<%= escape_javascript(render :partial => "mentoring_model/goals/display_goal_progress", locals: { goal: #associated_goal, goal_status: goal_status, connection_and_reports_page: true }) %>");
var percentage_content = <% content_tag(:span, goal_status.to_s+"%", :class => "pull-right cjs-mentoring-model-goal-progress-#{#associated_goal.id}") %>;
jQuery(goal_progress_display).replaceWith(percentage_content);
}
else
{
$(progress_from_goals).html("<%= escape_javascript(render :partial => "mentoring_model/goals/display_goal_progress.html.erb", locals: {goal: #associated_goal, goal_status: goal_status}) %>");
}
<% end %>
This is the final solution which helped me to resolve my issue.
<% if #associated_goal.present? %>
<% goal_status = #associated_goal.completion_percentage(#required_tasks) %>
var progress_bar_selector = jQuery("#cjs_show_goal_progress_bar_<%=#associated_goal.id.to_s%>");
var progress_from_goals = jQuery("#cjs_show_goal_progress_from_goals_<%=#associated_goal.id.to_s%>");
var goal_progress_display = jQuery(".cjs-mentoring-model-goal-progress-<%=#associated_goal.id.to_s%>");
if(progress_bar_selector.length > 0){
progress_bar_selector.replaceWith("<%= j(render :partial => "mentoring_model/goals/display_goal_progress", locals: { goal: #associated_goal, goal_status: goal_status, connection_and_reports_page: true }) %>");
<% percentage_content = content_tag(:span, goal_status.to_s+"%", :class => "pull-right cjs-mentoring-model-goal-progress-#{#associated_goal.id}") %>
goal_progress_display.replaceWith("<%= j(percentage_content) %>");
}
else{
progress_from_goals.replaceWith("<%= j(render :partial => "mentoring_model/goals/display_goal_progress.html.erb", locals: {goal: #associated_goal, goal_status: goal_status}) %>")
}
<% end %>

using if statement in each-loop

I have a loop
<% _.each(kw, function (x) { %>
now I want to use a if statement which checks a string
<% if ( <%=x%> == "condition") { %>
<div>..</div>
<% } %>
but doesn't work :( any ideas?
another question:
<script>
var maxKW = $("[id^='kalenderwoche']:first").attr("id");
</script>
is declared above and what would be the correct if statement when I want to use maxKW
<% if ( <%=x%> == maxKW) { %>
<div>..</div>
<% } %>
would be great if you could help me :)
<% if (x === "condition") { %>
<div>..</div>
<% } %>
Pass maxKW as attribute to the compiled template:
var maxKW = $("[id^='kalenderwoche']:first").attr("id");
var html = _.template('your html')({maxKW:maxKW});

Categories