I've created a pdfs directory in app/ for invoices and purchase orders (I'm using prawn). Naturally, I want the NumberHelper to be available. Whats the best way to do this?
You could
include ActionView::Helpers::NumberHelper
in the classes you use to generate the pdfs
Related
I like to keep things organized in my applications and since I have only been coding in ROR for a year. I have known how to use the app/controllers/concerns directory for custom modules to be included in a controller.
This is great however I am wondering if there is a way to add sub folders to the concerns folder to allow for better organization.
For example app/controllers/concerns/members/profile_methods.rb and the module is named 'ProfileMethods'. If I include 'ProfileMethods' RubyMine see's the module and allows me to include it, including offering it as an option in the tooltip pane. However the Rails Server says that 'ProfileMethods' is an undefined constant if it is in a sub folder of app/controllers/concerns .
Is there something I have to add to the rails application configuration? Any input would be great as it seems a little illogical that you could not further organize the concerns directory with sub folders.
The best way to keep things organized is to name the module the following way. That way you have your code organized in both files and code itself.
module Members
module ProfileMethods
extend ActiveSupport::Concern
...
end
end
This way it will load your modules fine. Other one option is to tweak eager_load_paths in config/application.rb for instance.
I am using Ruby on Rails 3.2.2 and I would like to organize and share custom helper-view methods as-like I made for my view files. That is, in my app/views directory I have a shared folder where I put all shared templates and I would like to have a shared folder (intended to be used the same "sharing way" but for helper files) also in the app/helpers directory.
However, my doubts are:
Is right to share helper methods instead of putting those in the ApplicationHelper module (even if those helper methods are specific for shared views and not directly related to any model or controller)?
How can I load modules present in the app/shared/helpers directory in order to make those available to views?
Is there some prescriptions to this approach?
In Rails, helpers are actually global. Which means that you can call a user helper in a, say, posts view. So you don't really have to pollute ApplicationHelper, just divide them in the best way possible, and just use them normally.
I'm trying to write a plugin, and among the tasks I want to perform I want to be able to call route helper methods from within the plugin. For instance, if I have map.resources :user, I want to be able to call user_path(:id => 1) from my plugin. I keep getting undefined method user_path error.
In rails 3, you can do this using Rails.application.routes.url_helpers, but I don't seem to be able to find an alternative for rails 2. Including ActionController::UrlWriter does not help. Any ideas?
I'm using rails 2.3.4 and i can use my regular path helpers in the controllers and views of my plugins, at least within the ones i tested.
I can't use them in the lib files for the plugins, but that's because the helpers aren't available outside the controllers (the views are dealt with inside the controllers so they can use them too). The lib files (the meat of the plugins) tends to be modules and classes which get loaded into the model environment.
Can you provide more details about what you're trying to do?
You should be able to do:
app.user_path(1)
Some functionality within my rails application looks better as if it was a separate "module", that should be accessed via require. For example, assume it's a function to calculate Fibonacci numbers.
The functionality is independent on rails application and can be reused in other projects, so it should not be stored near application controllers and models, I suppose. But since I'm not going to detach it into separate project, thus placing it to vendor folder seems like not the right thing.
Where should I place it then?
Rails < 5
Before Rails 5, the place to put reusable code such as this is in the lib directory. However you do not need to require anything as lib is already in the load path and it's contents will be loaded during initialization.
If you need to extend an existing class, you define your module first and then include it by sending it as a message to the class you wish to extend, e.g.
module MyExtensions
def self.included base
base.instance_eval do
def my_new_method
…
end
end
end
end
ActiveRecord::Base.send :include, MyExtensions
Rails 5+
The answer is for Rails 5 onwards. TLDR: app/lib is the convention now.
Note all the above answers were written before Rails 5.
\lib
Rails 5 discourages you from using \lib. While you are discouraged from using \lib you still can as long as you add \lib to eagerloading.
# config/application.rb
config.eager_load_paths << Rails.root.join('lib')
\app\lib
Nesting lib off app is now a common convention because any directory off app is automatically eagerloaded.
Reference
Rails guide - autoloading and eagerloading
Stackoverflow - confusing about autoload_paths vs eager_load_paths in rails 4
Stackoverflow - Why use app-lib instead of lib in rails
There is a lib directory in RoR projects which fits well for that purpose - I place common bits of code in form of "libraries" there. Anything from extending ActiveRecord classes to reusable utility methods.
I'll often put stuff in lib, it turns out that anything under lib is in the load path and doesn't need to be required at all.
edit: After Steve's comment, removed the bit about having to require the files. Also, removed a couple requires from some of my code :P
Where is the best location to put a plain old Ruby class in a Rails application? I'm not sure if it should go in the helper, model, or controller folder or should I create another folder to handle Ruby classes.
In the /lib folder is fine.
The question really is, what purpose does it serve? Is it data-like stuff? model for that. Is it a helper class that adds functionality to other classes or is a utility? Probably lib. Etc...
It would help to know what your class is trying to accomplish, but in general /lib is a decent enough location.