Refactoring a large routes.rb file in rails 4 - ruby-on-rails

I'm in the process of upgrading a rails 3 app to rails 4.0.1.
In my rails 3 app I have the following code in the my application.rb to use multiple route files.
config.paths["config/routes"] += Dir[Rails.root.join('config',
'routes', '*.rb').to_s]
but that throws an exception when I try to use the same thing in rails 4.
Any tips?

In one of my larger applications I use the following segment of code inside of my config/routes.rb file.
class ActionDispatch::Routing::Mapper
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
end
end
YourApplication::Application.routes.draw do
# Loads config/routes/common.rb
draw :common
# Loads config/routes/another.rb
draw :another
end
Rails 4 initially had support for draw :routeName but it was removed as it did not show any improvements. (I dont know ^.^) You can check out the git commit doing so here: https://github.com/rails/rails/commit/5e7d6bba79393de0279917f93b82f3b7b176f4b5

Check out this SO answer: rails 4: split routes.rb into multiple smaller files
Looks like this ability was deprecated in Rails 4.

I don't know how big your application is. But you should look into routing concern in rails 4, if you need some proper refactoring with Rails route.
Mo' files, mo' problems.

Related

Rails route sometime got an error not found

My Rails route sometimes get a not found error and resolves itself after a few seconds.
undefined local variable or method owner_root_path' for #<Owner::SessionsController:0x00007f30408d46f0>
/myapp/app/controllers/owner/sessions_controller.rb:30:increate’
/ruby/2.5.0/gems/actionpack-5.1.6.2/lib/action_controller/metal/basic_implicit_render.rb:4:in send_action'
/ruby/2.5.0/gems/actionpack-5.1.6.2/lib/abstract_controller/base.rb:186:inprocess_action’
My routes config
# routes/owner.rb
Rails.application.routes.draw do
constraints subdomain: /^owner/ do
root to: "owner/top_pages#show", as: :owner_root
...
end
end
# application.rb
config.paths["config/routes.rb"] = %w(
config/routes/owner.rb
config/routes.rb
).map {|relative_path| Rails.root.join(relative_path)}
Does anyone know why it happened?
Rails 6.1 guides introduced the draw macro which can be used to split a large route file into smaller files. You can read about it here: Breaking up very large route file into multiple small ones
I don't know whether your version of Rails supports this macro. If it doesn't, then you can easily define a draw method yourself, using the source. It will be something like this:
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
end
P.S: You should definitely check out the commit that introduced this method/macro.

Why does Rails 4 say Missing Helper file, though it is there?

I wrote a project in Rails 3. There are three controllers, three views, three helper files (very small project).
One of the helper files, which probably is causing the problem:
> cat helpers/my_helper.rb
module MyHelper
require 'some_gems'
...
def my_function ()
... #some functionality
end
end
Now I moved to Rails 4 (updated all the staff).
Still when just starting the app rails s -b localhost -p 3000 and going to localhost:3000 in browser I get an Exception:
AbstractController::Helpers::ClassMethods::MissingHelperError in WelcomeController#index
Missing helper file helpers/my_helper.rb
What is the problem? The file is there, why Rails still missing it?
It could be that the helper could not be loaded because of missing gems from which it is trying to require libraries. Please check that your Gemfile includes all gems that you need for this helper.

Custom Views Scaffolding in Rails Engines

I'm trying to get custom scaffolding working from my engine.
I followed some tutorial on customizing Rails 3.2 scaffolding in a normal Rails App and put my customized templates in the engines /lib/templates/erb/scaffold directory but they don't get picked up by the app that includes the engine. Any suggestions?
Update:
I also tried to override the Rails ScaffoldGenerator's source_path and tried some other paths to put my template in, like:
lib/rails/generators/erb/scaffold/templates
zarazan's answer got me most of the way there, but there are a couple of things wrong with it. Here's what worked for me:
class Engine < Rails::Engine
config.generators do |g|
g.templates.unshift File::expand_path('../../templates', __FILE__)
end
end
Note that this goes in the generators section, not app_generators, and that the path is slightly different.
Also, I think the correct path to store your templates is lib/templates/erb/scaffold, optionally replacing erb with whatever language you are using (like haml or slim.) I know this works for slim. The file names are {_form,edit,index,new,show}.html.erb.
In the file that you declare your engine use this command:
class Engine < Rails::Engine
config.app_generators do |g|
g.templates.unshift File::expand_path('../templates', __FILE__)
end
end
It should shift the preference of what template folder Rails uses by default.
Now just put the template files in lib/templates/erb/scaffold/template_name.erb
Where template_name is one of the following: _form.html.erb, edit.html.erb, index.html.erb, new.html.erb, show.html.erb
Once you include the gem you should be able to use the rails generate scaffold command as normal.
Here is an example of an engine that overrides the default scaffolding in rails:
https://github.com/brocktoncg/gemboree
This is where the template directory is located:
https://github.com/brocktoncg/gemboree/tree/master/lib/templates/erb/scaffold
Are you talking about a controller template? Than you are using the wrong directory. Save your template at
lib/templates/rails/scaffold_controller/controller.rb
Have a look at http://xyzpub.com/en/ruby-on-rails/3.2/templates.html for an example.

Losing namespace information in a rails namespaced model

When you create a namespaced model with rails scaffolding, you get two files. For example, this scaffold:
rails generate model Staff::Location name:string address:string
Generates these files:
/app/models/staff.rb
module Staff
def self.table_name_prefix
"staff_"
end
...
/app/models/staff/location.rb
class Staff::Location < ActiveRecord::Base
...
I am running into problems when in development mode where rails unloads the Staff module and never reloads it. This causes several annoying bugs such as Location not able to access it's table due to the missing table_name_prefix. The problem seems to crop up when I don't access the models directly, such as through a polymorphic relationship.
I can't seem to get the module loaded on a consistent basis. Is this the best practice way to do namespaced models? If it is, what am I missing?
Although I wasn't able to reproduce the problem in Rails 3.2.2, I've run into something like this before. The generic way to hack around this problem in development mode is through an ActionDispatch callback. Add this to config/environments/development.rb:
MyApp::Application.configure do
ActionDispatch::Callbacks.before do
load Rails.root.join('app', 'models', 'staff.rb')
end
end
Anything you do in that block will be executed before each request, so make sure you're only doing it in development mode.† Otherwise, you're going to suffer a performance hit in production.
I logged a message inside the staff.rb file and within the Staff module itself, and both messages appeared in the log for each request.
† I tried using the to_prepare callback, since that seems to be the documented way to execute code before each request only when cache_classes is false. But that only seemed to execute after restarting the application. There's at least one other open Stack Overflow question regarding this, although he's using a slightly different syntax than I used. If you can get to_prepare to work, I'd suggest that instead of before.
About a year later, I have finally found the answer to this question. This answer is specifically for rails 3.1. I am not sure if it is a problem in rails 3.2.
The problem occurs when setting up a model. If scaffolding is used, no helper file is generated. This would normally be in /app/helpers/staff/location_helper.rb. There are two ways to setup this file:
module Staff::LocationHelper
...
end
module Staff
module LocationHelper
...
end
end
In rails 3.1, specifically for helpers, you must use the first solution. You do not have to use it for other modules that use a namespace in other parts of the rails project. In fact, some structures in ruby require the second solution.
If you use the second solution when declaring a helper, in certain cases the Staff module in the helper file will override the module in /app/models/staff.rb. It will silently replace it with the empty Staff module in the file. This does not happen 100% of the time because helpers are not always loaded.

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