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
Related
Question: Using Rails 5 & Minitest-Rails is there a way to default new Rails apps to default to Spec-style testing?
I teach TDD and it's annoying to have to have the students convert each time we make a new app.
You could create a template.rb file with following configuration:
gem_group :development, :test do
gem "rspec-rails"
end
after_bundle do
`rails g rspec:install`
end
And then build a new Rails project using the following command
rails new my_app -T -m /path/to/template.rb
It will build a new Rails application, add Rails RSpec gem to its Gemfile and execute the install step for RSpec.
Otherwise you could provide a pre-built Rails Git repository and build on top of that.
References:
Rails Application Templates — Ruby on Rails Guides
rspec/rspec-rails: RSpec for Rails-3+
Looks like you've already done the hard work of answering your question. Though if you're teaching a class with an opinionated group of test gems, and a modified test_helper.rb and a modified application.rb, you may wish to consider writing your own gem that your students can add to their Gemfile. The gem could have the test gems you want as dependencies, and then they can install everything else they need with something like:
bin/rails generate <gem_name>:install
Here's a gem I wrote that you can fork or modify or just use as inspiration.
https://github.com/schadenfred/testable
I actually stole your config code for the above gem, which you can see expressed in inside a generator that lives here:
lib/generators/installer/install_generator.rb
It looks like in config/application.rb you just have to add:
config.generators do |g|
g.test_framework :minitest, spec: true
end
However there's not an automatic way to make Minitest-Rails default to spec style testing.
I could go to rspec, but would rather stay with Minitest for the moment as we teach our students Minitest from the beginning.
Ok so #sonna had 90% of what I was looking for.
I ended up with help creating a .railsrc file with
-d postgresql
-m ~/.template.rb
And a template with:
# .template.rb
# Gems we've talked about in class, but which have absolutely nothing to do
# with setting up spec-style testing.
# Included here for convenience.
gem_group :development do
# Improve the error message you get in the browser
gem 'better_errors'
# Use pry for rails console
gem 'pry-rails'
end
# Add some extra minitest support
gem_group :test do
gem 'minitest-rails'
gem 'minitest-reporters'
end
# Add some code to some files to support spec-style testing
# For these injections, indentation matters!
inject_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do
<<-'RUBY'
# Force new test files to be generated in the minitest-spec style
config.generators do |g|
g.test_framework :minitest, spec: true
end
RUBY
end
# Things to do after all the gems have been installed
after_bundle do
# Run rails generate minitest:install
generate "minitest:install", "--force"
# Add minitest reporters support. This must be run after
# rails generate minitest:install, because that command
# changes test/test_helper.rb
inject_into_file 'test/test_helper.rb', after: 'require "minitest/rails"' do
<<-'RUBY'
require "minitest/reporters" # for Colorized output
# For colorful output!
Minitest::Reporters.use!(
Minitest::Reporters::SpecReporter.new,
ENV,
Minitest.backtrace_filter
)
RUBY
end
end
This sets up my project with postgres for DB, and Minitest-rails using spec-style tests and includes minitest-reporters.
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 have a rails 4 app. I have no tests at the moment, but when I created the app I didn't skip the default test unit so there are some empty test files (and other default settings) in my app.
Now I would like to use rspec + capybara, but don't know what the necessary extra steps are to properly install those and make sure the testing will work fine. I saw some answers on stackoverflow but thoose were pretty old.
As far as I know the installation looks like this if test unit is skipped on creation:
group :development, :test do
gem 'rspec-rails'
end
group :test do
gem 'capybara'
end
then
rails g rspec:install
Can sby tell me what the extra steps are?
Follow these steps:
add code to your_app/config/application.rb file:
config.generators do |g|
g.test_framework :rspec
end
add below code to your_app's Gemfile:
group :test, :development do
gem 'rspec-rails'
end
save it, and run bundle install to install rspec gem
Initialize the spec/ directory
rails generate rspec:install
Use the rspec command to run your specs:
bundle exec rspec
Hopefully it helps.
When I try to run rails generate cucumber:feature, I get an error - Could not find generator cucumber:feature.
In my gemfile:
group :test do
gem 'cucumber-rails'
# database_cleaner is not required, but highly recommended
gem 'database_cleaner'
end
The cucumber generate command should looks like
rails generate cucumber:install
after that go to features folder
and create any file name like article_search.feature within write your features ,scenarios
look like as https://github.com/cucumber/cucumber-rails/tree/master/features
for more environment fix
check features/support/env.rb
and for your step definitions go to
features/step_definitions folder
look like this https://github.com/cucumber/cucumber-rails/tree/master/features/step_definitions
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.