ruby injections in coffeescript execute despite conditions - ruby

In RoR project -> views -> ... -> create.js.coffee
code
$("<%= j(render partial: 'invoice_equipment/invoice_equipment', object: #invoice_equipment) %>").appendTo("#invoice_equipment_list")
executes always, not dependent of conditions before.
In this file I want show some message for user, if he try create record, that already exists in DB.
If remove that line from file, another js working correctly line by line.
All file:
if "<% #invoice_equipment %>"
$("<%= j(render partial: 'invoice_equipment/invoice_equipment', object: #invoice_equipment) %>").appendTo("#invoice_equipment_list")
M.toast({html: "<%= I18n.t('invoice_equipment.added') %>", displayLength: 3000, classes: 'rounded'})
else
M.toast({html: "<%= I18n.t('invoice_equipment.already_added') %>", displayLength: 3000, classes: 'rounded'})
$('#new_invoice_equipment')[0].reset()
$('#invoice_equipment_equipment').focus()
#invoice_equipment can be nil or ActiveRecord. But in both times RoR try render partial and fall if #invoice_equipment is nil.
So, I think, injected ruby code executed before creating js file.
I'm right?

It's likely because you're using a mix of javascript and rails templating in your conditional. Try reformatting your conditional so it's not using the javascript if:
<% if #invoice_equipment.present? %>
# Your code to render when the object is present
<% else %>
# Your code to render when the object is missing
<% end %>

Related

can someone explain this code in chef template

can someone explain this code in chef template? I would like to add an if condition to just variable2 . how do I approach this?
<%= #variable1 %>/*[!.][!g][!z] <%= #variable2 %>/*[!.][!g][!z] {
some random data in file
}
Ok, so in a Chef template (.erb file) anything outside the markers <%= ... %> and <% ... %> is plain text, i.e. it will be rendered as-is on the destination.
So /*[!.][!g][!z] inside the .erb file has no meaning. Once it is rendered on the destination, it may have relevance. And what it will do at the destination depends on what type of file it is, and what's going to use the file.
<%= #variable1 %> is to interpolate the value of variable1. Using <% ... %> will allow you to run Ruby script, i.e. if/else conditions, for loops etc.

Ruby on Rails if/else condition not working

I have a Rails application view where I want to display one of two partials depending on the ENV variable setting. For some reason, the condition is always evaluated to false so the campaign_active partial is displayed. I've changed the ENV to true and tried switching the rendering statements around and found that only the statement after else gets executed. What am I missing?
Here's the view:
<% if Rails.application.config.ended %>
<%= render "users/campaign_ended" %>
<% else %>
<%= render "users/campaign_active" %>
<% end %>
Here's the application.rb setting:
config.ended = ENV['CAMPAIGN_ENDED'].to_s == 'true'
Here's the .ENV file:
CAMPAIGN_ENDED=true
The campaign setting in ENV is set to true so I expect the condition in my view to be true and render the campaign_ended partial. But instead, it renders the campaign_active partial. Now if I switch the statements around and put campaign active ahead of campaign ended, then the campaign ended partial renders.
Please double check that you are loading your .env file in the config/application.rb with
Dotenv::Railtie.load
before you actually use the environment variable in this line
config.ended = ENV['CAMPAIGN_ENDED'].to_s == 'true'
Remember that you need to restart the Rails application after changing the .env file.

How to concatenate #something inside a render function in a template

I have this function in my template:
<%= for {element, id} <- Enum.with_index(MyProject.PageView.Recursion.buildElements(#header_linkNumber),1) do %>
<%= render FabricaASA.ComponentView, #header_linkType,
button_id: "#{id}",
button_mainStyle: #header_mainStyle
%>
<% end %>
Now I would like to concatenate, on my right side, #header_mainStyle + id so that from other template, for each created element, I could pass: header_mainStyle1, header_mainStyle2,...header_mainStyleN
Also, on the left side, where I have button_mainStyle: I would like to concatenate #header_linkType + _mainStyle: so that I could dynamically change it to, link_mainStyle: or button_mainStyle:
Up to now I wasn't able to do it properly...
I'm afraid you are doing something wrong if you need such thing. Maybe there's a simpler solution...
Anyway: since some version of Phoenix (I'm sorry I don't know which one precisely, maybe 1.0?), #-variables are stored in #conn.assigns map and you can access them by name there. In older versions, these variables were macros and this kind of magic did not work.
So you can try to put this into the controller:
def index(conn, _params) do
render conn, "index.html", [var1: "var1"]
end
and this into the page template:
<p>var1: <%= #var1 %></p>
<p>assigns:</p>
<%= for i <- 1..10 do %>
<p>var<%= i %>:<p>
<pre><%=
varname = "var#{i}" |> String.to_atom
inspect(#conn.assigns[varname]) %>
</pre>
<% end %>
...you will see var1 to var10 bindings (screenshot: http://postimg.org/image/4b4790cjz/). But it's little bit black magic and probably wrong approach.

ERB file not replacing ENV VAR

I have a simple erb file I'm trying to run and it doesn't seem to replace when I have the environment variables set:
<% if ENV['DEFAULT_ROUTE'] %>
default_route : "<%= ENV['DEFAULT_ROUTE'] %>",
<% else %>
default_route : "/dashboard/file/default.json",
<% end %>
I've checked the environment variable and everything should be working based on what I've read, however I always get the else printed. Any idea what I'm doing wrong?
if you can't tell I'm not a big Ruby dev

Rails 3 refactoring issue

The following view code generates a series of links with totals (as expected):
<% #jobs.group_by(&:employer_name).sort.each do |employer, jobs| %>
<%= link_to employer, jobs_path() %> <%= "(#{jobs.length})" %>
<% end %>
However, when I refactor the view's code and move the logic to a helper, the code doesn't work as expect.
view:
<%= employer_filter(#jobs_clone) %>
helper:
def employer_filter(jobs)
jobs.group_by(&:employer_name).sort.each do |employer,jobs|
link_to employer, jobs_path()
end
end
The following output is generated:
<Job:0x10342e628>#<Job:0x10342e588>#<Job:0x10342e2e0>Employer A#<Job:0x10342e1c8>Employer B#<Job:0x10342e0d8>Employer C#<Job:0x10342ded0>Employer D#
What am I not understanding? At first blush, the code seems to be equivalent.
In the first example, it is directly outputting to erb, in the second example it is returning the result of that method.
Try this:
def employer_filter(jobs)
employer_filter = ""
jobs.group_by(&:employer_name).sort.each do |employer,jobs|
employer_filter += link_to(employer, jobs_path())
end
employer_filter
end
Then call it like this in the view:
raw(employer_filter(jobs))
Also note the use of "raw". Once you move generation of a string out of the template you need to tell rails that you don't want it html escaped.
For extra credit, you could use the "inject" command instead of explicitly building the string, but I am lazy and wanted to give you what I know would work w/o testing.
This syntax worked as I hoped it would:
def employer_filter(jobs_clone)
jobs_clone.group_by(&:employer_name).sort.collect { |group,items|
link_to( group, jobs_path() ) + " (#{items.length})"
}.join(' | ').html_safe
end

Resources