Override the default Rails model template - ruby-on-rails

I want to override the default model file that's generated with rails generate model. I've created a template based on this file, but I can't figure out where to put it. Other answers seem to suggest /lib/templates/rails/model/model.rb or /lib/templates/rails/model/model_generator.rb, but neither of those do anything - I put the template in that location but when I run rails generate model ModelName it gets ignored.
Am I going about this the right way? Where should I put the template?

Solved: I wanted lib/templates/active_record/model/model.rb.

You probably need to put it in "lib/rails/generators/active_record/model/templates/model.rb". Here's the rails' default one: https://github.com/rails/rails/blob/master/activerecord/lib/rails/generators/active_record/model/templates/model.rb

Related

Rails 5 missing template that was added to lookup

I have added into application.rb string
config.paths['app/views'] << 'app/views/cabinet'
and created a view 'app/views/cabinet/index.html.slim'.
But when I go to route localhost:3000/manager/pages (It uses layout manager if it make sence), Rails gives the error
Manager::PagesController#index is missing a template for this request format and variant.
What I'm doing wrong?
I am not sure what you are trying to do by overriding the mapping for the location of app/views but it doesn't sound like a good idea to me.
Without knowing more about your code I would suggest you remove that config line from application.rb and simply use:
render 'cabinet/index'
from PagesController#index action.

Custom Validation Classes in Ruby on Rails

I am trying to make a custom validation class in ruby on rails, however I cannot figure out how to make it modular.
I followed this example:
Validator Class Example
For the second example on that page titled: "Validate object single states with an isolated custom validator" how can I put the PreventFutureDateValidator class into a separate file? I cannot figure out where these files would be placed in Ruby on Rails.
These kindo of files are normally stored in /lib.
It's common to put validator classes in /app/validators/

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.

Forcing override of file with template() in Rails generator?

I have myself a generator that, as part of a set of other operations, needs to set up a model class with a bunch of mixins, defaults, comments, etc.
I want to be using the same rails g model ... code (I'm calling invoke from my generator), but the problem is that there's a conflict because my template and the model generator's template are trying to splat each other:
$ be rails g entry_form karaoke events full_name:string group_name:string
create app/controllers/karaokes_controller.rb
create app/views/karaokes/show.html.erb
create app/views/karaokes/thanks.html.erb
route resource :karaoke
create app/models/karaoke_entry.rb
invoke active_record
create db/migrate/20111004004008_create_karaoke_entries.rb
conflict app/models/karaoke_entry.rb
Overwrite app/models/karaoke_entry.rb?
(enter "h" for help) [Ynaqdh]
Any recommendations how to get around this?
(The best I've come up with is to maybe move my model file creation to the bottom, and find some way to force template / copy_file to go ahead and overwrite the file without bothering the user, but I can't see any pre-existing way of doing this.)
Append force: true or skip: true to the template call:
template "model.rb", "app/models/#{model}.rb", force: true
Some group brainstorming turned up a way to work around this problem.
You can't override (in so far as I can tell), but you can tell the model generator to skip files that already exist. This works:
# Create the model definition from a template:
template "model.rb", "app/models/#{model}.rb"
# ... Later, get Rails to create everything else:
Rails::Generators.invoke("model", ["Example", "title:string", "--skip"])
(I would still welcome a way to allow template to override files.)

Ruby on Rails model inside namespace can't be found in controller

I'm new to rails and can't figure out this issue...
I have a controller
Admin::Blog::EntriesController
defined in app/controllers/admin/blog/entries_controller.rb
And I have a model called
Blog::Entry
defined in app/model/blog/entry.rb
When I try to access my model from the controller, I get a "uninitialized constant Admin::Blog::EntriesController::Blog" from this line:
#blog_entries = Blog::Entry.find(:all)
Clearly it is not finding the namespace correctly which is odd because according to what I have read, I have placed my model in the correct folder with the correct syntax.
Any ideas on how I can fix this?
Thanks
Try:
#blog_entries = ::Blog::Entry.find(:all)
It's currently looking for the wrong class. Using :: before Blog will force it to look from the top level.
It is now 2011 and we are in Rails 3.1 territory, but this issue still arises. I just ran into it with a namespaced controller referencing a non-namespaced model, but only when there were no rows for that model in the database!
Prefixing the model name with :: fixes the problem.
You can achieve a custom table name by using
set_table_name('foo')
at the top of your model.
As for multiple namespaces, you might be able to get away with using
polymorphic_path(#the_object)
to generate your urls as it does more basic inference (in my experience at least, maybe form_for uses it under the hood).
Yeah, from looking at the code form_for uses polymorphic_path under the hood.

Resources