How to Set a Variable from a Helper Method for Inclusion in a SASS SCSS Stylesheet

How to set a variable from a helper method for inclusion in a SASS SCSS stylesheet?

Rename your SASS file to foobar.css.scss.erb. Then include the helper module you need that contains the helper method by placing this line at the top:

<% environment.context_class.instance_eval { include MyHelper } %>

Then you can use the helper method with erb

.foo {
color: <%= cool_color %>
}

Ruby on Rails - Using helpers inside css erb file gives undefined method

Helpers are not available by default to templated .css files. They are intended to help in view construction only. However, you can try the workaround mentioned here using an initializer:

Rails.application.assets.context_class.instance_eval do
include YourHelperModule
end

Alternatively, if you only need this for one or a few files, you can use the solution mentioned here, by adding this code to the top of the .css.erb file:

<% environment.context_class.instance_eval { include YourHelperModule } %>

Change background image url by time

try this way

<script type="text/javascript">

var currentTime = new Date().getHours();
if ( currentTime> 6 | currentTime<9) {
if(document.body) {
document.body.background = "sunrise.jpg";
}
}
else if (currentTime>=9 &¤tTime < 20) {
if(document.body) {
document.body.background = "daytime.jpg";
}
}
else if (currentTime>20 &¤tTime < 24) {
if(document.body) {
document.body.background = "sunset.jpg";
}
}
else {
if (document.body) {
document.body.background = "nighttime.jpg";
}
}

</script>

source : http://www.sitepoint.com/forums/showthread.php?744436-Changing-div-background-image-based-on-time

is this an improper use of MVC?

I did this exact same thing about a month ago. I wrote an amazing extention to Enumerable to be able to split the collection into groups of N, and then realized ActiveSupport already provided exactly that (almost).

Take a look at in_groups(number, fill_with = nil) it will split an array into number groups, perfect for the view :)

Sass: Browser vendor prefixes

Sounds like you want to use the Compass box-sizing mixin. Your SASS file would look like this:

@import "compass/css3/box-sizing";

* {
@include box-sizing(border-box);
}

And would compile to this:

* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box; }

You can see the other CSS3 Compass mixins here. Note, though, that Compass doesn't include prefixes like -ms-box-sizing, for instance, since IE8+ has implemented it without a prefix. If you really want those extra properties, this is how you'd do it:

@import "compass/css3/shared"

* {
@include experimental(box-sizing, border-box, -moz, -webkit, -o, -ms, not -khtml, official);
}

Problems referencing root in Capybara test

You should be calling

visit root_path

and you should be requiring rails_helper, rather than spec_helper. since rails_helper is where the url helpers get included - 'config.include Rails.application.routes.url_helpers'. To fix the error you get from doing that we'd need to see the contents of config/initializers/environment_variables.rb



Related Topics



Leave a reply



Submit