Ruby on Rails if/else condition not working - ruby

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.

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 injections in coffeescript execute despite conditions

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

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

How can i add a space between a Label and a combo box in Ruby code

I am very new to Ruby, i need to add a space between a Label and Combo box in ruby file.
Sample code:
def render_search_combo_box(project, selected_value=nil)
return unless User.current.logged?
s = "<select id='search_options' name='search_options'>"
end
i have tried adding 'nbsp;' before 'select' but no result came.
Thanks in advance.
I think, it`ll be better to solve your problem using CSS.
For example (in css file):
#search_options {margin-left: '10px'}
However, why you not just use rails form helpers to build your form?
You can achieve same result like this (in view):
<% if User.current.logged? %>
<%= select_tag "search_options" %>
<% end %>

Is `x = x` a documented or acceptable way to initialize a local variable in Ruby?

I have noticed that in Ruby the code x = x initializes a local variable x with nil if x is not defined. This can be used, for example, in an ERB template to check if a local variable is defined and has a "true-ish" value:
<% if header = header %>
<h1><%= header %></h1>
<% end %>
So, is this a feature or a bug? Is it documented somewhere?
This may work, but I think it disguises your intent.
If you want to check if something is defined then you should say that
if defined?(header) && header
however in partials I do normally default all values at the start of my template
eg (with haml)
- # input header is optional
- header ||= nil
- if header
%h1 This is the header
%p this is the rest of the partial
This is neither a feature or a bug. You can simply test for the truthiness of the local variable by calling it, as if it is not initialized it will return nil by default.
<% if header %>
<h1><%= header %></h1>
<% end %>

Resources