Ruby on Rails javascript assets - ruby-on-rails

I have a problems with javascripts assets in Ruby on Rails.
Descripttion:
I have two files in app/assets/javascript folder.
"constans.js" include a constant array "var FEATURES = new Array["A","B","C"]"
"route.js.erb" <%= FEATURES[1] %>
Now , I'm implementing my function in "route.js.erb" but I can't access the "FEATURES" array ?
I searched on Google but can't not find the solution.
So, anybody can help me? Thanks!
( my first question in stack overflow , sorry for my bad english)

use
window.FEATURES = new Array["A","B","C"]"
in constants.js
and make sure that the constants.js is being loaded.

There are several important factors:
Scoping of the variable
Calling the variable in ERB
Scope
First, you need to ensure your variable is scoped globally. To do this, you've declared the variable in constants.js, so you need to ensure this is called before the routes.js.erb file. You should also take #user3243476's advice & append it to the window object:
#js/constants.js
window.FEATURES = new Array["A","B","C"]
ERB
Secondly, you're calling routes.js.erb (which is fine), but inside you're calling <%= FEATURES["1"] %>. Problem. This is calling a Rails constant, not the JS one. This means even if your variable's scope is global, you're trying to call one which doesn't exist.
You'll need to do this:
#js/routes.js.erb
alert(FEATURES["1"]);

Related

How to delete a helpers file in Rails 5?

I'm new to Rails, and after using rails g controller Users to generate a users controller, I decided to remove (i.e., rm) the generated helper in app/helpers/users.rb, because I realized I didn't need it. It seems ugly to keep a bunch of empty files around to me. This broke my app. When I try to visit any page in my, or run a test, I get this error: Couldn't find UsersHelper, expected it to be defined in helpers/users_helper.rb.
I fixed it by manually re-creating that file, but how do I get rid of it? Is that just not supported?
Edits based on questions people asked
The helper does not appear to be referenced explicitly anywhere, grep -Ri UsersHelper . only returns results for the module itself and a bootsnap cache file. The same command with :helper 'user' or 'helpers/users' returns nothing.
If Rails is telling you that it can't find UsersHelper then it means that the module is called somewhere else in the project. When the page shows you the couldn't find message, it should also indicate the offending file where UsersHelper is being included (same with the test output). If you remove this reference, you should have no problem removing the module (or directory).
I sort of figured out the issue, but I still don't understand it.
I am using some url helper functions: user_url(:id) and new_user_url, and these apparently require the helpers to exist even though they are not explicitly defined there. If anyone can explain in more depth what's going on, I'd appreciate it.

Using SASS variables in Rails code

I am afraid that this question is simply stupid, but... is there any option to use SASS variables in rails .erb files?
I defined a few colors in my variables.css.scss and I wish to use their values in my views or helpers in Rails. Maybe Rails can see some compiled sass resources or something?
Thank you for answers!
You can do it the other way around, ie using ruby code in your css/sccs code. It's a bit tricky, but it may help you :
First declare the color as a ruby constant :
# Put this into config/initializers/constants.rb for example
module Constants
FOO_COLOR = '#123456'
end
Next, rename the variables.css.scss into variables.css.scss.erb and use the constant created at the previous step
$fooColor: <%= Constants::FOO_COLOR %>
Finally use the color in your other scss files
#import "_variables";
#foo {
background-color: $fooColor;
}
And you can also use the Constants::FOO_COLOR in your Rails code too.
Be careful, you may be using precompiled assets in production. It will work with a constant as shown below, but it won't work if you want to change the value, or get it from a DB.

application wide global variable

In Rails, where should I define the variable which can be recognized by every layer of Rails stacks.
For example, I would like to have a CUSTOMER_NAME='John' variable which can be accessed in helper, rake task, controller and model. Where should I define this variable in Rails app?
I am using Rails v2.3.2
In an initializer in /app/config/initializers all .rb files in here get loaded, I usually create one called preferences.rb for things like this.
See: http://guides.rubyonrails.org/configuring.html#using-initializer-files
An alternative approach is to set a key on the config object in config/application.rb, like so:
MyApp::Application.configure do
# ...
config.my_key = 'some "global" value'
end
You can then access my_key from anywhere in your app with just this:
MyApp::Application.config.my_key
Also, Mike Perham has described a similar, though a more comprehensive approach in his blog post.
You want a true global constant? Use ::COSTUMER_NAME.
You want a true global variable? Use $COSTUMER_NAME (discouraged).
You want a request-global variable? Use the Hash in the #env method.

Random method named cv_member_url()

I'm a total noob to rails and I'm trying to understand a project I'm working on. I've found a method named cv_member_url in one of the views, but I can't for the life of me figure out where it is defined... One thing I do know about rails is that it is a very flexible language so it could be some sort of gem creating this method.
Any ideas where this method may have come from? (or better yet, how I can add others)
Thank you!
Do you have a model called CvMember? If so, the method is probably a named route for that model. See here for more info:
http://guides.rubyonrails.org/routing.html#paths-and-urls
To see all your named routes, you can run
rake routes
Those are named routes which are automatically defined based on what you have in routes.rb. *_url should be used in the controller, and *_path should be used in the views. Here's some more info from the official rails guide.
Assuming you can run this in development: You should put a breakpoint on it and step inside. Most likely it is dynamically defined or maybe in plugin.

What is the simplest way to make a group of methods available to several Rails applications

tl;dr
What's a simple way I can create a view helpers module I can use in many apps?
What file, located where, loaded how?
I'm on Rails 2.3.11.
I have a bunch of view helper methods. Here's an example of one of them:
# Render form input fields to add or edit a ZIP code.
def render_zip_code_fields( model_instance )
# Bla bla bla ...
end
I have about 20 or so that I've developed over the years and often use in various applications. I'd like to wrap them all up in one file that I can just drop into and app and then be able to call them in my views.
Why not just copy-and-paste them into application_helper.rb? That just doesn't feel right to me. It seems like it should be a separate file.
In fact I tried creating in /lib...
# /lib/my_view_helpers.rb
module MyViewHelpers
# ...
end
And then in application_helper.rb I put...
include MyViewHelpers
But I got a lot of "uninitialized constant MyViewHelpers errors. Maybe a syntax error? I don't think I need to require my_view_helpers.rb first because it's in /lib. Everything in there gets loaded automatically, right?
So what's the right way to do this optimizing for simplicity?
Sorry this is so long. I get verbose when I'm tired.
As of Rails 3, /lib is no longer on the default load path. You will need to put the following line in the Application class in config/application.rb.
config.autoload_paths += ["#{config.root}/lib"]
An alternative would be to drop the file in app/helpers since it is a helper, after all.

Resources