Questions tagged [rails-generators]

Rails generators use templates to create stub files for models, controllers, views, unit tests, migrations and generators itself.

Rails generators use templates to create stub files for models, controllers, views, unit tests, migrations and even generators itself to create customized generator behaviour.

40 questions
11
votes
4 answers

Rails - using `rails generate model` to specify non-nullable field type

According to the rails documentation http://guides.rubyonrails.org/migrations.html 2.3 Supported Type Modifiers says it should be possible to modify fields to allow or disallow NULL in the column, and that it's possible to do this on the…
Alan
  • 297
  • 3
  • 12
9
votes
3 answers

How to test generators with RSpec

Is there a recommended way of testing Rails generators with RSpec? The only solution I've found is the Generator Spec gem, that hasn't been updated in over two years.
Sebastian
  • 2,154
  • 1
  • 26
  • 42
7
votes
1 answer

Rails::Generator is not creating factory in spec folder

I am trying to generate a new factory inside a module and I basically required require 'rails/generators' and I am calling the command below. Notice that it creates the file inside the test folder. Rails::Generators.invoke 'factory_girl:model',…
5
votes
1 answer

How to override Rails' default migration generator template

I need to override these migration templates: https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb and…
Surya
  • 15,703
  • 3
  • 51
  • 74
3
votes
2 answers

(Rails 4.2) How to trigger scaffold generation from View?

How can I trigger a scaffold generation from a view? For example, let's say I have a method like this: def scaffold_generation system "rails g scaffold TodoList task author" end How can I make a button on my "example_page.html.erb" trigger this…
2
votes
1 answer

Can I change how Rails creates migrations

TL;DR; In the past (a few years ago now), I have used custom generators within Ruby to change how things are created when say you run rails g model Users. My question today is whether or not you can change how Rails generates a migration file. What…
2
votes
1 answer

Using class_name in a custom Rails generator

I have a basic custom generator that looks like this, that inherits from Rails::Generators::NamedBase in a Rails 5.1 app class NotificationGenerator < Rails::Generators::NamedBase source_root File.expand_path('../templates', __FILE__) def…
Msencenb
  • 5,675
  • 11
  • 52
  • 84
2
votes
0 answers

undefined method `load_defaults' on rails generate migration and rails console

I'm getting this error when I run: rails generate migration AddRoleToBuyer role:integer The error: /usr/lib/ruby/vendor_ruby/rails/railtie/configuration.rb:95:in `method_missing': undefined method `load_defaults' for…
Murilo Azevedo
  • 331
  • 1
  • 2
  • 7
2
votes
1 answer

How to write a rails generator that would override the default scaffold javascript file?

Background: Currently writing a gem One of its features is that when rails generate my_gem_name:install is run, it (should) override the default javascript scaffold generated file (which is produced by rails generate scaffold…
Jay-Ar Polidario
  • 6,463
  • 14
  • 28
2
votes
0 answers

Ruby Gem InstallGenerator Could not find generator ':install

I have just tried to make a ruby gem for my css grid system. However I am at the point where when rails g --help it show me: Running via Spring preloader in process 62748 Usage: rails generate GENERATOR [args] [options] General options: -h,…
2
votes
1 answer

Add Migrations to a Rails Engine's Install Generator

In my rails 5 engine I want to include the installation of the engine's migrations with the custom install generator I have at: myengine/lib/generators/myengine/install_generator.rb This generator currently looks like this: module Myengine module…
2
votes
1 answer

Generating proper paths in namespaced Rails scaffolds

When you use rails generate scaffold admin/user --model-name=User or rails generate scaffold_controller --model-name=User it generates almost everything in a namespaced fashion. You get app/controllers/admin/users_controller.rb with your controller…
Ben G
  • 26,091
  • 34
  • 103
  • 170
2
votes
0 answers

Rails 4.2 Generator ERB Template methods and variables, where do they come from?

In the ERB for scaffold generators in Rails 4.2 from where do the variables / methods come from in the template? Things like singular_table_name and attributes in the below '_form.html.erb' template. Are they methods? variables? where can I find a…
2
votes
3 answers

"rails generate controller" is not creating a controller

I am trying to run this in the terminal: rails generate controller welcome index However, it is not creating a controller. The process finished with exit code 0. Any ideas? EDIT My Environment RUBYGEMS VERSION: 1.8.10 RUBY VERSION: 1.9.3…
simo
  • 23,342
  • 38
  • 121
  • 218
1
vote
1 answer

How can I replace the template of the scaffold_controller generator?

When we use the command rails g scaffold Post title content it will generate many files according to some templates located inside railties. There is even a command that we can copy all templates to our application: rake app:templates:copy But there…
Arthur Felipe
  • 1,404
  • 1
  • 13
  • 20
1
2 3