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.
Related
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.
I am trying to create a Javascript-free app (don't ask why...) and have run into the apparently common problem of the :delete method not working.
I found this question which told me I have to use Plug.MethodOverride.
So, I have added
plug Plug.Parser, parsers [:urlencoded, :multipart]
plug Plug.MethodOverride
to the Html pipeline in routers.ex (I am guessing I should probably move it to somewhere more specific later, but for now, I just wanted it to work quickly). However, the form generated by the resources helper still does not do anything. Do I have to change it to something else? Or am I missing a step in adding the Plug
thanks #Dogbert for pointing me in the right direction in the docs, I had not understood that link_to has an underlying form_for. To make it work I replaced the <% link_to ..%> by the following code
<%= form_for #conn, model_path(#conn, :delete, model), [multipart: true, method: "delete", as: :custom_delete], fn f -> %>
<%= submit "Delete_nojs" %>
<% end %>
with
plug Plug.Parser, parsers [:urlencoded, :multipart]
plug Plug.MethodOverride
in the router pipeline this enables the method override.
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.
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 %> ?
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' %>