Using Conditionals with variables in EJS - javascript

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.

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.

How to correctly use Plug.Override Method in phoenix to enable :delete without Javascript?

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.

javascript inside html-erb files issue

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 %> ?

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