I'll try use $server_facts['environment'] in epp template, but he dont work correct.
My epp code:
<% if $::facts[environment] == 'production' { -%>
# this is production server
<% } else { -%>
# this is NOT production server
<% } -%>
code run without error but always use section after else. Please help fix it.
all work fine after use $::facts[agent_specified_environment]:
<% if $::facts[agent_specified_environment] == 'production' { -%>
# this is production server
<% } else { -%>
# this is NOT production server
<% } -%>
Related
I want to run this block only a condition is satisfied
<% if condition %>
<% progressive_render do %>
SLOW CODE HERE
<% end %>
<% end %>
IF condition = true
SLOW CODE should be runnned wrapped by "progressive_render"
IF condition = true
SLOW CODE shoud be runned not wrapped by progressive_render. So runned anyway.
You can replace the if-else condition with a guard clause (plus unless condition):
<% SLOW CODE HERE unless condition %>
<% progressive_render { SLOW CODE HERE } %>
I'm trying to figure out an error message in my production mode. It's referring to a syntax error saying I'm missing keyword end and pointing to a line around about this block of code.
Do I need to write <% end %> again? Do I need it once for the if and once again for the else?
<ul id="nav" class="row nopadding cd-side-navigation">
<% if logged_in_as?(#profile) %>
<%= render 'profiles/menu_owner', profile: #profile %>
<% else %>
<%= render 'profiles/menu_visitor', profile: #profile %>
<% end %>
</ul>
The code seems to be fine as it is currently. You only need one <% end %> for if/elsif/else block of condition.
There is only one <% end %> required per if condition. Please paste in log so we can have closer look.
I'm wondering if there's any way for a Rails asset to vary according to the environment.
Something like:
function log(m) {
<% if Rails.env=='development' %>
console.log(m)
<% end %>
}
And similarly, you could show a special message or color scheme by making environment-specific features in the stylesheet.
(I realize assets are compiled in production, which is why I'm not expecting to do this with any arbitrary Ruby code while the server's running. But I wonder if there's a way to do it with the environment at least.)
see Asset Pipeline, Preprocessing
Dynamic Javascript
in your javascript manifest file app/assets/javascripts/application.js
//...
//= dynamic_js
//...
in app/assets/javascripts/dynamic_js.js.erb
function log(m) {
<% if Rails.env=='development' %>
console.log(m)
<% end %>
}
Dynamic CSS
in your stylesheet manifest: app/assets/stylesheets/application.css
/*...
*= dynamic_css
*/
in app/assets/stylesheets/dynamic_css.css.erb
.environment-color {
<% if Rails.env == 'development' %>
color: <%= 'red' %>
<% else %>
color: <%= 'white' %>
<% end %>
}
I have a a helper that contains a simple on and off switch. I know I have it working because it's working on other pages. However, on this particular page it won't work.. I think its because theres an end within the if else, so it ends the if else early. Here's the code:
I believe this part is working:
<% if popup == "off" %>
<% content_for :main do %>
<% end %>
This part not so much:
<% if popup == "off" %>
<% end %> << this end should be displayed if popup = off
<% end %>
You could do this:
<% if popup == "off" %>
<%= "<% end %>" %> << this end should be displayed if popup = off
<% end %>
or try this:
<% if popup == "off" %>
<% end %> << this end should be displayed if popup = off
<% end %>
If you just want the word end to be displayed, don't enclose it in tags. Anything enclosed in tags is interpreted as Ruby code, anything not is printed exactly as it is.
<% if popup == "off" %>
end << this will now be interpreted as text, not ruby code
<% end %>
ERB (and Ruby) doesn't work like that.
I think you're treating it like you are trying to end an HTML tag instead of an end to a Ruby block, and that you want everything in between those two code segments to run in the content_for block.
Here's what you need. Everything in between will be included in the content_for block:
<% if popup == "off" %>
<% content_for :main do %>
your block code will be evaluated here.
<% end %>
<% end %>
Seems all the suggestions of doing <%= "<% end %>" %> results in a syntax error.. May seem like easy way out by ended up just restructuring my app and got rid of the requirement of <% content_for :main do %>
I'm having a problem getting the syntax correct. The html helper returns true or false.
<% if (<%= Html.SecurityTrim("Admin")%>) { %>
<span>Only for accounting</span>
<% } %>
What do I need to change to get this to compile correctly?
Supposing that Html.SecurityTrim() returns bool,
<% if ( Html.SecurityTrim("Admin") ) { %>
<span>Only for accounting</span>
<% } %>
Sounds like your Html.SecurityTrim() returns a bool.
In this case, then no need to escape out of code, just use it as your condition to test.
<% if (Html.SecurityTrim("Admin")) { %>
<span>Only for accounting</span>
<% } %>