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 %>
}
Related
I'm using rails config gem to set feature flags.
Settings.yml
enable_grading: <%= 'true' == ENV['ENABLE_GRADING'] %>
When I try to hide/show some part of the template based on the feature flag, it responds inconsistently.
<% if Settings.enable_grading %>
<div class='grade_tabs'>
...
</div>
<% end %>
So here, even if Settings.enable_grading is true, the div block is not being rendered. But if I modify this to:
<% if Settings.enable_grading == true %>
<div class='grade_tabs'>
...
</div>
<% end %>
now this starts working!(the content is rendered). This is working if we use some sort of ruby interpolation just before the condition check.
<%= Settings.enable_grading %>
<% if Settings.enable_grading %>
<div class='grade_tabs'>
...
</div>
<% end %>
This is really weird. What is wrong in this code? I was using this gem to set feature flags before also which was working well.
I know this is definitely wrong, but I cannot seem to get sub menu links to work on mobile devices, and other posts suggest edditing the bootstrap.min.js file.
However, since I am using the gem "twitter-bootstrap-rails" I do not know where the file is and even if it would work to change it.
Ideas?
This is my code, omitted some parts.
<%= nav_bar :fixed => :top, :brand => image_tag('logo.png'), :responsive => true do %>
<% if user_signed_in? %>
<%= menu_group :pull => :right do %>
<%= drop_down "Scan" do %>
<%= menu_item "Android", 'http://sasfad' %>
<%= menu_item "iPhone", 'zxing://asfasdf' %>
<% end %>
<% end %>
<% end %>
You can't and you shouldn't edit those files. But you can fork the gem, edit the bootstrap.min.js file and use your forked gem in your Gemfile.
My recommendation is not to use the twitter-bootstrap-rails gem as the only thing it does is adding for you the assets to the asset pipeline.
Now, I have this JQuery in my application.html.erb
But I see this in every html of my web site. So I want to put this in refresh_post.js, which is created in /assets/javascripts
But as you know, application.html.erb calls all JavaScript file in the directory. Because I use many and putting this
<%= javascript_include_tag "application" %>
How can I call this JQuery only when user is logged in. and Where should I make refresh_post.js at so that it won't be loaded unless user is logged in?
The purpose of doing this is for SEO. I don't want to put JavaScript in HTML.
views/application.html.erb
<% if current_user %>
<%= javascript_tag do %>
jQuery(document).ready(function () {
refreshPartialPost();
setInterval(refreshPartialPost, 1000)
});
function refreshPartialPost() {
$.ajax({
url: "/posts/refresh_partial",
type: "GET",
dataType: "script",
});
}
<% end %>
<% end %>
How about this for your application layout:
<%= javascript_include_tag 'application' %>
<% if current_user %>
<%= javascript_include_tag 'refresh_post' %>
<% end %>
Have a look at this, Even though this is for CSS same can be applied for js
HTH
I am writing a Rails 3.2.1 application and I have some javascript code I'd like to put in for a single action view. It simply calls a jquery plugin and starts a countdown, but I'd like to write it in coffee script and I feel like the asset pipeline is the correct tool to do this.
Also I need access to the variables passed by the controller such as #question. How would I do this? I have looked into the coffeebeans gem but that only works for :remote=>true forms and links.
Your problem can be solved in different ways.
Add the assets dynamically
Add to our application helper the following method:
module ApplicationHelper
def include_related_asset(asset)
# v-----{Change this}
if !YourApp::Application.assets.find_asset(asset).nil?
case asset.split('.')[-1]
when 'js'
javascript_include_tag asset
when 'css'
stylesheet_link_tag asset
end
end
end
end
Call the helper method in your layout-file:
<%= include_related_asset(params[:controller].to_param + '_' + params[:action].to_param . 'js') %>
Create specific assets for your controller actions. E. g. controller_action.js
Use yield
Add <%= yield :head%> to your layout head
Include your assets from your action views:
<% content_for :head do %>
<%= javascript_include_tag 'controller_action' %>
<% end %>
Please see the Rails guides for further information.
To passing controller data to your javascript, you could do:
<%= javascript_tag do %>
window.error_message = '<%= j error_message %>';
<% end %>
Please see the RailCast Episode #324 for further information.
If there a way to include the content of a javascript file into a Rails view?
I know about Ruby's File.read, but I'm searching for some helper already done.
<%= javascript_tag do %>
<%= render :file => '/path/to/rails/app/path/to/javascript.js' %>
<% end %>
So you can include any javascript code located on your machine. But to be honest there's more appropriate way to do that kind of things.