I have a Rails command with specific commands and a custom template, that I'd like to share with my coworkers:
rails new my_app --template template.rb --database=postgresql --skip-bootsnap --skip-asset-pipeline --skip-javascript --skip-action-cable --skip-active-job --skip-action-mailbox --skip-active-storage --skip-action-text --skip-hotwire --skip-test --skip-system-test
# template.rb
gem "devise"
gem "hashie"
gem "pry"
gem "pry-byebug"
gem "slim"
gem "vite_rails"
# ... and much more
Is it possible to somehow include the provided command line arguments inside the template itself? My goal would be that you only need to write
rails new my_app --template template.rb
When I debug into the template, I see there are some methods like #delete_active_job_folder_if_skipping_active_job or #delete_action_cable_files_skipping_action_cable but for example, I don't find a way to specifiy the database or set the --skip-asset-pipeline option.
Any idea on how to include those command line options (I guess they are using Thor) inside the template? Or am I simply too late in the process at the time, rails processes the template?
Related
I'm running on windows 7 and I cannot install/create the rspec files and capybara which is needed to work on the assignment .
If you could finish the simple setup steps listed below and give me a link to the empty app repository so I could download to finish the assignment , I will be so gratefull to you .
by the way , I 'm getting the following errors in step 4 if someone could help . I have asked before but no one have answered ;(
Steps needed :
Create a new Rails application called todolists
Add the following specification to your Gemfile
group :test do
gem 'rspec-rails', '~> 3.0'
gem 'capybara'
end
Run the bundle command to resolve new gems
From the todolists application root directory, initialize the rspec tests using the rails generate
rspec:install command
[todolists]$ rails generate rspec:install
create .rspec
create spec
create spec/spec_helper.rb
create spec/rails_helper.rb
Add the following line to .rspec to add verbose output to test results.
--format documentation
Download and extract the starter set of bootstrap files.
1
|-- Gemfile
|-- db
| ‘-- seed.rb
‘-- spec
‘-- features
‘-- module3_action_pack_spec.rb
• overwrite your existing Gemfile with the Gemfile from the bootstrap fileset. They should be nearly identical,
but this is done to make sure the gems and versions you use in your solution can be processed by the
automated Grader when you submit. Any submission should be tested with this version of the file.
• overwrite your existing db/seed.rb file using the seeds.rb provided with the bootstrap fileset. The
bootstrap seeds.rb file contains some test data that will be useful during development and unit tests.
• add the spec/features/module3_action_pack_spec.rb file provided with the bootstrap fileset to your
todolists application. Within your application root directory, you will first need to create a corresponding
spec/features sub-directory to place the module3_action_pack_spec.rb file. This file contains tests that
will help determine whether you have completed the assignment.
6-run rspec
The problem is that rspec versions > 2.8.0 don't go well with Windows. Can you try to install an older version of rspec-rails? Include this in your Gemfile.
gem 'rspec-rails', '~> 2.8'
Run bundle install and then try
rails g rspec:install
I'm not at all familiar with Rails, but Google (although very unlike it) couldn't help me out this time.
I'm running an installation of Rails set up by somebody else.
This is what I get when I run rails g:
Usage: rails generate GENERATOR [args] [options]
General options:
-h, [--help] # Print generator's options and usage
-p, [--pretend] # Run but do not make any changes
-f, [--force] # Overwrite files that already exist
-s, [--skip] # Skip files that already exist
-q, [--quiet] # Suppress status output
Please choose a generator below.
Rails:
assets
controller
generator
helper
integration_test
jbuilder
mailer
migration
model
resource
scaffold
scaffold_controller
task
Bootstrap:
bootstrap:install
bootstrap:layout
bootstrap:partial
bootstrap:themed
Coffee:
coffee:assets
Jquery:
jquery:install
Js:
js:assets
TestUnit:
test_unit:plugin
You'll notice a couple of generators missing, amongst which performance_test, the one I'm trying to use.
Why is this list not complete? How can I install missing generators? I'm running version 4.0.3.
As of rails 4 performance tests are no longer part of the default stack. If you want to use performance tests simply follow these instructions.
Add this line to your application's Gemfile:
gem 'rails-perftest'
If you want to benchmark/profile under MRI or REE, add this line as well:
gem 'ruby-prof'
Now run bundle install and you're ready to go.
rails-perftest
I would be appreciated if you could help me in this problem:
I am using rails 3.2.11 and I have already installed
"gem installed scaffold"
I do not know why it is not working for me
main$ rails generate scaffold idea name:string description:text picture:string
It give me this error:
Usage: rails new APP_PATH [options]
generate command is for generating new code. scaffold is a type of generators, it is for generating models, controllers and views in existing application.
To create the project you have to use rails new APP_PATH [options].
The 'rails new' command creates a new Rails application with a default
directory structure and configuration at the path you specify.
This is my setup cheat sheet:
In the terminal run the following command:
rails new "app name" --database=postgresql --skip-test-unit
In the gem file add the following:
group :development, :test do
gem "rspec-rails"
gem "factory_girl_rails" `<- only put in test group, not dev`
gem "valid_attribute"
gem "shoulda-matchers"
gem "capybara"
gem "launchy"
gem "simple_form"
gem "pry-rails"
end
group :production do
gem 'rails_12factor'
end
Run ' bundle install ' to install the gems
rails generate rspec:install
rails generate simple_form:install
in spec_helper file require 'capybara/rails'
valid_attribute also needs --> require 'valid_attribute' in the spec_helper.rb file
capybara will need -> require 'spec_helper' in each of the test files in the directory /spec/features/filename_spec.rb
You should see a number of files and directories created for you. The most important of these is the spec directory.
Now you can create models: by tying the model generation process to the creation of the files and directories that RSpec wants, rspec-rails ensures that we have a testing environment that we need.
Add config/database.yml to /.gitignore file
I think you missed to get into your working folder before running the generate scaffold action.
cd "your_app" then run rails generate scaffold idea name:string description:text picture:string
I'm in my app folder, but the command rails s is not working. I read through quite a few posts on Stack Overflow, and most of them seem to be from users who are not in their app directory.
In addition, I built a few other apps. I checked those, and the Rails server works for all of those apps. This is the only one where I can't get it to launch.
Output of which rails:
/Users/jmcrist/.rvm/gems/ruby-2.0.0-p247/bin/rails
Output of rails s:
MacBook-Pro:first_app jmcrist$ rails s
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /Users/jmcrist/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
-b, [--builder=BUILDER] # Path to a application builder (can be a filesystem path or URL)
-m, [--template=TEMPLATE] # Path to an application template (can be a filesystem path or URL)
[--skip-gemfile] # Don't create a Gemfile
[--skip-bundle] # Don't run bundle install
-G, [--skip-git] # Skip Git ignores and keeps
-O, [--skip-active-record] # Skip Active Record files
-S, [--skip-sprockets] # Skip Sprockets files
-d, [--database=DATABASE] # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
# Default: sqlite3
-j, [--javascript=JAVASCRIPT] # Preconfigure for selected JavaScript library
# Default: jquery
-J, [--skip-javascript] # Skip JavaScript files
[--dev] # Setup the application with Gemfile pointing to your Rails checkout
[--edge] # Setup the application with Gemfile pointing to Rails repository
-T, [--skip-test-unit] # Skip Test::Unit files
[--old-style-hash] # Force using old style hash (:foo => 'bar') on Ruby >= 1.9
Runtime options:
-f, [--force] # Overwrite files that already exist
-p, [--pretend] # Run but do not make any changes
-q, [--quiet] # Suppress status output
-s, [--skip] # Skip files that already exist
Rails options:
-h, [--help] # Show this help message and quit
-v, [--version] # Show Rails version number and quit
Description:
The 'rails new' command creates a new Rails application with a default
directory structure and configuration at the path you specify.
You can specify extra command-line arguments to be used every time
'rails new' runs in the .railsrc configuration file in your home directory.
Note that the arguments specified in the .railsrc file don't affect the
defaults values shown above in this help message.
Example:
rails new ~/Code/Ruby/weblog
This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
See the README in the newly created application to get going.
I'm working through Hartl's Rails Tutorial, and he makes quite a few modifications to the gemfile. I am wondering if this might be the cause?
source 'https://rubygems.org'
gem 'rails', '3.2.13'
group :development do
gem 'sqlite3', '1.3.5'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.5'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.2'
group :production do
gem 'pg', '0.12.2'
end
It seems to think you are not in a rails directory (your output is saying the only valid way to use rails is with rails new).
Depending on your version, Rails identifies this differently. On 3.2, it checks for a file at script/rails. Now that 4.0 has been released, it looks for either script/rails or bin/rails (https://github.com/rails/rails/blob/207fa5c11ddf1cfd696f0eeb07d6466aae9d451e/railties/lib/rails/app_rails_loader.rb#L6)
Presumably you can get around this by creating the file rails in your script directory (if you do not have a script directory, create one in the root of your app):
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rails/commands'
Of course, it's worth wondering why you don't have this file in the first place. Might be worth making sure your rails is the version you want to be using first (rails -v if the version is newer, this post will show you how to create the new app using the older version).
Possible reasons:
you are not in a directory that contains a full rails app
your bin directory might me empty, try to run rake rails:update:bin (for Rails 4) or rails app:update:bin (Rails 5)
All the above answers didn't help me. What solved my problem for Rails 4 was to run command in the root directory of my application:
rake rails:update:bin
After that running rails s was running as expected.
If you use rvm or rbenv for instance to keep multiple ruby versions, maybe your default rails version for that specific ruby version is different than the project you are trying to run and therefore it's not being able to detect your application.
To make sure you are using the right rails version you can compare both results. This is what I've got:
$ rails -v
Rails 3.1.0
to
$ bundle exec rails -v
Rails 5.0.0.1
In this case, you can keep the default rails version and then use:
$ bundle exec rails server
Or install the specific rails gem to that very ruby version with:
$ gem install rails -v 5.0.0.1
$ rails -v
Rails 5.0.0.1
And then get it working with the less verbose command:
$ rails s
I hope this becomes helpful to other folks in the same situation!
You likely have not bundled your gems yet:
# from command line
bundle install
I had this problem, took me a few minutes to realize I'd forgotten to change active Ruby version with chruby. Different Ruby implied a different Rails version, which looked for the relevant file in another folder.
First check with your location path and then
bundle install
If still does not work, enter
/bin/bash --login
bundle install
This works for me.!!!(NOTE: run this commands into rails app)
rake db:migrate RAILS_ENV=development;
Run this command to generate /bin
rake rails:update:bin
OR
rake app:update:bin
=============================================
Then you will get this kind of screen(In that Go with Y)
bin/rails? (enter "h" for help) [Ynaqdhm] Y
Then you can start the server using
rails s
I'm not sure if this sort of thing is very common, but I keep finding myself trying to create gems that are just wrappers around a Rails application.
My gem will have a generator to create a config.ru but the Rails application will live inside the gem's lib directory. I need to know how to "embed" a Rails application and configure it so that it can be run inside the gem.
For example:
$ mygem new project
mygem created a directory called "project" with the following files:
project/config.ru
project/widgets/
project/foobars/
My gem will also generate some directories that will need to be added to Rails somehow so that I can access the code in those directories from the Rails app living inside the Gem.
Any help or advice you can give me would be appreciated.
To clarify, I'm not trying to create a Rails engine, or plugin to a Rails application. I'm trying to create a fully-fledged Rails application, but package it as a gem so that a user of my gem can run the gem (the rails app) without needing to know that it's using Rails behind the scenes.
Update: Okay, I've got a little bit working now. I've created the gem and generated the rails project inside the gem's lib directory.
$ bundle gem my_gem && cd my_gem/lib
$ rails new my_gem --skip-bundle
Which leaves me with:
my_gem/
my_gem.gemspec
bin/my_gem
lib/
my_gem.rb
my_gem/
version.rb # generated by bundler
# the rails app:
app/
config/
Gemfile
...etc
Since this gem requires Rails, I started adding the gems defined in the Rails Gemfile as dependencies in the gem's Gemspec, but I'm a little confused as to how to handle the assets group in the Gemfile.
# Rails Gemfile
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
end
# gemspec
Gem::Specification.new do |gem|
gem.name = "my_gem"
# ...
gem.add_dependency 'rails', '3.2.8'
gem.add_dependency 'sqlite3'
gem.add_dependency 'jquery-rails'
# how to add the assets group gems?
end
Try this and see if it helps you make progress.
Gems are just directories of files, and you can put whatever files you want into a gem.
Create:
Create a blank gem full-blown Rails project:
$ bundle gem my_gem
Then a Rails app:
$ rails new my_app --skip-bundle
Copy the Rails files into the gem:
$ cp -R my_app/* my_gem
Bundle everything into your Rails app:
$ cd my_gem
$ bundle install --binstubs --path vendor/bundle
$ cd -
Make the Rakefile have the gem tasks and the Rails setup:
#!/usr/bin/env rake
require "bundler/gem_tasks"
require File.expand_path('../config/application', __FILE__)
MyApp::Application.load_tasks
Verify that it starts:
$ rails server
Load Path:
To control where Rails looks for files, such as "external" configuration files, you can use the file config/application.rb with any directory paths like this:
# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{config.root}/../customdir )
Note the ".." which means go above the Rails directory. This gives you a path relative to the gem.
If you prefer you can specify an absolute path, for example if you know the user will always keep his external files in "~/myfiles/". You can also choose to use ENV vars to send in whatever directory you want.
If you read about load path capabilties, look for lines that are shorthand for adding a directory to the front of the load path because you may want to put your external diretories first:
$:.unshift File.dirname(__FILE__)
Gem Build:
Edit my_gem.gemspec to add your own description, homepage, summary, etc. then build:
$ gem build my_gem.gemspec
Successfully built RubyGem
Name: my_gem
Version: 0.0.1
File: my_gem-0.0.1.gem
Now your Rails app is packaged as a gem.
The config.ru should be a typical Rails one. No special changes AFAIK.
When your user wants to install your app:
$ gem install my_gem
The gem will install in the user's typical gem directory. If you want to adjust this, see this page on rubygems: http://docs.rubygems.org/read/chapter/3
Crate:
You may also want to investigate the Crate project:
Crate: Packaging Standalone Ruby Applications
http://www.slideshare.net/copiousfreetime/crate-packaging-standalone-ruby-applications
Rack:
To use config.ru here is the typical Rails setup:
# Rails.root/config.ru
require "config/environment"
use Rails::Rack::LogTailer
use ActionDispatch::Static
run ActionController::Dispatcher.new
For your project, you want to require some files before Rails. You'll want to learn about the Ruby "require" and how it finds files using LOAD_PATH.
The easy way:
# Rails.root/config.ru
require_relative 'filename'
require "config/environment"
Or to put the user's custom directory up couple directory levels:
require './../../filename' # not the best for security
Or to use an absolute path, read about File.expand_path:
File.expand_path(__FILE__)
Or to use the current directory and put it on the load path:
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'filename'
Lots of choices for you to consider. Hope this helps!
What about the question, "How am I going to run the Rails application inside the gem?".
A Rails application has controllers and views to run a web server. What you need are actions to create, list, update, and destroy. Exposing these actions without a web server is essentially having such methods in a class. That's a normal standard type of gem in the first place.
So maybe your questions is really, how do I write a gem where I have ActiveRecord, and the other Rails stuff.
First, you need to make your gem dependent on the Rails gems you need. You do this in the gemspec file for your gem.
Then it really is just a matter of your gem code doing a require of the right Rails gems you need.
I'm not sure if this will help, as I read through everything and I couldn't find the motivation behind why you were doing this. One of the reasons I came up with was making something that can be used on a desktop environment. In that case you could try using something like Bowline. If you just want to provide an application that others can download and use and install themselves, then you can probably assume they can follow at least basic developer kind of instructions and you could just provide the whole app on github or as a zip file. See an example of someone else doing something similar over on Fat Free CRM's github page.