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
Related
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 %>
I have the following yaml file in my data dir:
---
type:
- config_setting1:
foo: bar
- config_setting2:
foo: bar
My .erb template looks like this:
conf {
<% settings = YAML.load_file('/etc/puppetlabs/code/environments/example/data/conf.yaml') -%>
<% settings['type'].each do |val| -%>
<%= val %>
<% end -%>
}
When I run puppet on my agent machine I end up with this:
conf {
{"config_setting1"=>nil, "foo"=>"bar"}
{"config_setting2"=>nil, "foo"=>"bar"}
}
My end goal is to get the output to look like this:
conf {
config_setting1 {
foo: bar
}
config_setting2 {
foo: bar
}
}
I know I have some clean up to do on my template to actually get things to output that way, but I'm more focused on the how than the end result at the moment. As you can see I'm familiar with using the ['type'] on the end of the settings to navigate through the nested hash, and I realize I could create this structure pretty easily if I hard coded it but I want to understand how to use it iteratively. I've been attempting to follow the Puppet Documentation on iterations but their examples don't work even when you copy them verbatim... which makes things a little difficult. How can I call pull out a single piece of data in a nested yaml file like I have? Either just the key or just a specific value? I tried something like:
<% settings['type'].each do |val| -%>
<%= settings['val'] %>
<% end -%>
and multiple variations of this but I couldn't find the right syntax to get what I wanted. I've also tried having something along the lines of <% settings['type'].each do |index, value| -%> but I was unable to get any results I could use out of that either. If anyone could point me in the right direction I'd appreciate it. I'm open to being told that I'm going about this entirely the wrong way as well; if there is a better way to get at this data I'm all ears.
Another question that's less important, but still irks me - in my load_file I have the absolute path... is there a way to use relative?
Amazing how typing something out will answer your own question. I realized there was a pretty easy solution. If we take my template:
conf {
<% settings = YAML.load_file('/etc/puppetlabs/code/environments/example/data/conf.yaml') -%>
<% settings['type'].each do |val| -%>
<%= val %>
<% end -%>
}
and on line three replace <% settings['type'].each do |val| -%> with <% settings.keys.each do |val| -%> I'm able to get what I'm looking for. I'd still be interested if there is a better way to do this though, either how I'm loading via yaml or otherwise.
I am trying to create a loop only if index is defined. But it looks like
erb can't handle a loop within a if clause.
<% if(#index) %>
index <% index_files.each do |i| %> <%= i %> <% end %>;
<% end %>
Expected Result was:
index index.html index.php
or
""
Syntax error i got:
My flat approach failed as expected:
<% if(#index_files) %> try_files <% end %> <% index_files.each do |i| %> <%= i %> <% end %>
I defined index_files as undef => broke the each loop
I defined an empty array => since an empty array is defined it didn't work.
Maybe I can check the length of index_files?
Or do I need a complete different way to solve the problem?
I'm doing the same and it works for me, also for nginx ;).
For example:<% if #proxy_ignore_headers %> proxy_ignore_headers<% proxy_ignore_headers.each do |i| -%> <%= i %><% end -%>;
That works like a charm, the only difference with you is using () for the if condition, but I bet puppet supports (). It's weird, maybe you had pressed a bad combination generating a character that can't be seen but it's messing with your code, try writing all from scratch just in case.
You can see the full template here
Good luck
At first glance you just need to change
index_files.each
to
#index_files.each
I have an app where users can sign up with Facebook, in which case I take the Facebook image, or they can sign up using regular authentication, in which case they're uploading a photo using CarrierWave gem.
Because of some sort of conflict, I had to create a column in the database (:image)to store the url to the Facebook image, and then a different column (:dimage) to store the url for the image if they signed up using the non-facebook sign up.
So when it comes time to display the user, I'm checking to see if there's an :image, and, if not, then displaying the other :dimage.
However, I don't want to require them to upload an image, in which case, I want to display an anon image (here represented by the rails.png). However, the Rails.png isn't showing up (just a broken image path), so I'm assuming there's some sort of error with my if else syntax because the image_tag("rails.png") is taken straight from the api
<% if user.image %>
<%= image_tag user.image %>
<% elsif user.dimage %>
<%= image_tag user.dimage_url(:thumb).to_s %>
<% else %>
<%= image_tag("rails.png") %>
<% end %>
The generated html on the rails.png
<img alt="Assets" src="/assets/">
Use the present? method to check if the attribute is not empty:
<% if user.image.present? %>
<%= image_tag user.image %>
<% elsif user.dimage.present? %>
<%= image_tag user.dimage_url(:thumb).to_s %>
<% else %>
<%= image_tag("rails.png") %>
<% end %>
You'll save yourself a headache or two because in Ruby the only falsehoods are false and nil. This means that the empty string "" will actually return true.
You need to look at look into user.image.nil? user.image.empty? and user.image.blank? depending on what you're using and apply the same to the rest of your conditional. You need to find out specifically what your looking for or the if else won't give you what you want. I would try empty first which checks if something is nil or is an empty string.
When you create a new rails application, rails stores its logo in PROJECT_PATH/app/assets/images folder. Could you check if that image exists?
I have a controller with an API request showing all my Google Docs.
feed = client.get('http://docs.google.com/feeds/documents/private/full').to_xml
feed.elements.each('entry') do |entry|
puts 'title: ' + entry.elements['title'].text
puts 'type: ' + entry.elements['category'].attribute('label').value
puts 'updated: ' + entry.elements['updated'].text
puts 'id: ' + entry.elements['id'].text
# Extract the href value from each <atom:link>
links = {}
entry.elements.each('link') do |link|
links[link.attribute('rel').value] = link.attribute('href').value
end
puts links.to_s
end
So, I can see the results in my console but how do I get them into my view?
I tried with something like this, but that doesn't work (I changed my variable in the controller to an accessor of course)
<% feed.elements.each('entry') do
|entry| %> <%
entry.elements['title'].text %> <%
end %>
First, in your controller, make feed an instance variable. IE: it should be:
#feed = client.get..... instead of feed = client.get....
If that doesn't fix it... I don't know your API for sure, but I suspect you may need to be using:
<% #feed.elements.each('entry') do |entry| %> <% entry['title'] %> <% end %>
Note: entry['title'] instead of entry.elements['title'].text
What your current code indicates is that the feed is structured like this:
feed.elements[0].elements['attr'].text, when it's probably just feed.elements[0]['attr']
Does that make sense? Try that and see what happens.
If that doesn't work, just put: debug(#feed) in your view and copy and paste it to the end of your question. That'll help us figure out the right way to access this info.
Problem solved. Because I use 'puts' in the controller to show the content of the feed in the console I also have to change that for the view. Of course, puts is equal to <%= ... %>.
<ul>
<% #feed.elements.each('entry') do |entry| %>
<li><%= 'title: ' + entry.elements['title'].text %></li>
<% end %>
</ul>