python python - 7 months ago 37
Ruby Question

How to define ruby code inside a haml file present in javascript folder

For my project, I am using

Ruby on Rails
and I have a
file present in
folder. I want to call a ruby class from the
file but unfortunately I am not able to do that.

- if #ruby code
{{ item | timeformat }}
- else
{{ item | differentTimeFormat }}

Whenever I start the server, it's is showing it can't access the ruby class. I can easily access the ruby class in other
files but not the one present in javascript folder. Can anyone help me here?


Disclaimer: I'm bad at Angular (and didn't even touch version 2). What follows is not a best practice or anything.

So, you need to configure your angular view with some knowledge from ruby side. You can't conditionally render it and you can't call ruby from angular controllers (obviously). I suggest smuggling data via window object.

In an appropriate view in your application, put this JS snippet.

<script type='text/javascript'>
    window.jsEnv = {
      hide_hours_field: <%=[:hide_hours_field] %> 

Then you can reference that via $window object in angular


function MyController($window) {
  this.hideHours = function() {
    return !!$window.jsEnv.hide_hours_field;

MyController.$inject = ['$window'];

angular.module('myApp').controller('MyController', MyController);


.small-12.columns(ng-controller='MyController as vm')
      {{ item | timeformat }}
      {{ item | differentTimeFormat }}