javascript inside html-erb files issue - javascript

I ma trying to understand why this javascript inside my erb file is not working
<%= javascript_tag do %>
var tmp= "<%=j render :partial => "layouts/alert_error_message", :object=>#campaign %>";
<% end %>
I get this message
compile error
/var/www/gitorious/app/views/campaigns/new.html.erb:32: syntax error, unexpected ')'
...r.concat(( javascript_tag do ).to_s); #output_buffer.concat ...
/var/www/gitorious/app/views/campaigns/new.html.erb:38: syntax error, unexpected kENSURE, expecting ')'
When I use instead <% javascript_tag do %>, the render method is correctly processed but the tmp variable is not set in my javascript (console.log(tmp) returns undefined)
Edit : I wrapped the render method with escape_javascript and it worked but I still don't undestand why it is only working when I use <% javascript_tag do %>. Is it not suppose to work with <%= javascript_tag do %> ?

Related

How to Reference ruby code Inside a JS alert tag

I'm new to JS and was wondering if someone could help me figure this alert out?
<% if #candy.errors %>
<%= javascript_tag do %>
alert('<%= #candy.errors.full_messages %>')
<% end %>
<% end %>
This is all that comes up on the alert: []
<% if #candy.errors %> will always return a truthy value. The value it returns is an instance of ActiveModel::Errors. By truthy, I mean it will never return false or nil, and therefore your alert will always show...even if there are no errors with #candy.
You'll need to add .any? to return false if there are no errors.
<% if #candy.errors.any? %>
If my assumption that there are no errors with the #candy you are working with is correct, no alert should show when any? is added.
To manually add an error to #candy, add this line before <% if #candy.errors.any? %>...
<% #candy.errors.add :base, "Test" %>
If an alert pops up then the issue is simply that #candy has no errors and your call to #candy.errors.full_messages correctly returns an empty array.

Using Conditionals with variables in EJS

I have the following which is working as intended:
<%= employee.title %>, Support Member
I am trying to add a conditional such as:
<% if(!employee.title.includes('Junior')){ %>
employee.title
<% } %>
Support Member
However it seems that it no longer has access to "employee" as it prints out the literal "employee.title".
I have referred to this thread for syntax help but have had no luck. Does anyone spot my issue? I couldn't find documentation help online.
You need to do:
<% if(!employee.title.includes('Junior')){ %>
<%= employee.title %>
<% } %>
Support Member
Check Docs.

How to Render JS and Iterate Over Array With Rails Controller

I have a button on a view that triggers a controller action, which runs a bunch of stuff and returns and array of strings (each string is formatted as a div ex; "Hello"). I then want to take this array and add each element to the page on which the button was clicked.
Controller Method:
def test
#id = params[:id]
if #id == 'run'
#res = Preflight.run
respond_to do |format|
format.js {render :js => 'run'}
end
else
render #id
end
end
run.js.erb File:
$('#header3').show();
$('#header2').hide();
<%= puts 'made it to the js file' %>
<% #res.each do |line| %>
$('#results').append(<%= line %>);
<% end %>
I know that #res is returning the array as intended, I'm unable to get the JS to trigger. Any ideas? It doesn't run the show/hide so I think the bug has to do with how I'm attempting to call the js file, but I'm unsure what I'm doing wrong here.
Do you use the remote: true option in your HTML, when you call the test action?
Remember that when you want to output a JS string you need to wrap it in quotes:
$('#results').append("<%= #res.join() %>");

Returning javascript from rails helper

I am having problems returning javascript from helper functions. I am unable to get the following test case to work:
function in helpers/application_helper.rb
def show_stuff
return '$("div#flash").html("<p> stuff </p>");'
end
now I try to call this helper function in general.js.erb
<%= show_stuff %>
Here is the output
$("div#flash").html("<p> stuff </p>");
I've also tried show_stuff.html_safe and raw show_stuff and had no luck. I feel the issue is Rails auto-escaping html but have been unable to find a solution.
ERB is escaping the characters in your outputted Javascript.
To prevent this happening use the raw method in your template.
<%= javascript_tag do %>
<%= raw(show_stuff) %>
<% end %>
I've added the javascript_tag method around this for context.

(rails3) passing ruby variables to javascript file

When rendering a javascript file (not RJS, just a regular JS file) from a rails controller, is there a way to pass variables to it, which can then be used by functions in the JS file being called? I've tried :locals => {} but that doesn't work.
If you're rendering inside a js file you should use rjs. If you really don't like rjs (understandable) then how about rendering your variables first, then including your script?
<% javascript_tag do %>
var my_var = <%= #my_var.to_json %>;
<% end %>
<% javascript_include_tag 'your-js-file-that-expects-my_var-to-exist.js' %>

Categories