rails generate model - ruby-on-rails

I'm trying to follow instructions from the book "Head First Rails" and on page 50 it says to create a model but I am unable to create a model using the rails command.
When I type this at this prompt: localhost:~ home$
rails generate model ad name:string description:text price:decimal seller_id:integer email:string img_url:string
I get this:
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /Users/home/.rvm/rubies/ruby-1.9.3-p125/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.
localhost:~ home$
I am using Rails -v 3.2.8
and Ruby 1.9.3p125

The code is okay but you are in the wrong directory. You must run these commands inside your rails project-directory.
The normal way to get there from scratch is:
$ rails new PROJECT_NAME
$ cd PROJECT_NAME
$ rails generate model ad \
name:string \
description:text \
price:decimal \
seller_id:integer \
email:string img_url:string

The error shows you either didn't create the rails project yet or you're not in the rails project directory.
Suppose if you're working on myapp project. You've to move to that project directory on your command line and then generate the model. Here are some steps you can refer.
Example: Assuming you didn't create the Rails app yet:
$> rails new myapp
$> cd myapp
Now generate the model from your commandline.
$> rails generate model your_model_name

For me what happened was that I generated the app with rails new rails new chapter_2
but the RVM --default had rails 4.0.2 gem, but my chapter_2 project use a new
gemset with rails 3.2.16.
So when I ran
rails generate scaffold User name:string email:string
the console showed
Usage:
rails new APP_PATH [options]
So I fixed the RVM and the gemset with the rails 3.2.16 gem , and then generated the app again
then I executed
rails generate scaffold User name:string email:string
and it worked

You need to create new rails application first. Run
rails new mebay
cd mebay
bundle install
rails generate model ...
And try to find Rails 3 tutorial, there are a lot of changes since 2.1 Guides (http://guides.rubyonrails.org/getting_started.html) are good start point.

Related

Can not acces localhost:3000 ruby on rails ubuntu

I have been trying to access or at least show my server in my Rails by running the command rails s or rails server and it does not work:
At first it told me I had to install new gems and to run the command bundle install to upgrade them and I did. It installed everything "correctly".
Now instead, I just get some usage help instead of Rails (for example commands I can use). I thought that Rails was out but I run the command rails new myApp and it does create me one and the bundle installs ok. I just want to start my app and try localhost:port, and that to open my app.
It happens the same when I try to generate something, for example rails g model OneModel (or generate instead of g) and for anything I want to generate. It seems it isn't understanding my commands.
I am noob new to ubuntu and far beyond that new to Rails and ruby. If you can help me I'd be so glad. Here's the output I get:
agustin#agustin:~/Agustin/myapp$ rails s
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /usr/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.
agustin#agustin:~/Agustin/myapp$
You have to run rails s from the project root directory.
eg, If you create a rails application, say rails new my_app, it will create a skeleton application my_app in the current directory. Now you have to cd to that directory to start the server

rails generate controller command is not executed

I am trying to generate a controller in my rails app using the following command:
rails generate controller CreditCard
but it simply returns me the following:
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /export/data0/home/tanya/.rvm/rubies/ruby-1.9.3-p448/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.
Even running 'rails console' return me the same message.I don't understand why the generate command does not work.
I am currently using rails 3.2.13 and ruby 1.9.3.
Thanks a lot for your help.
I think you are not in rails application directory. Please check path and run command again.
This message generally occurred when you try to run generate controller command out of application directory.
I think you are not running the command inside Rails root directory.You have to navigate to the application before running the rails generators.If you have an application named blog then follow the below commands
cd blog
rails g controller posts
Thanks a lot for all you suggestions. What I did was simply to create a new application using rails new new_app and copy the files of my original rails application to this new directory.
After I did that, it seems to work fine. I must have messed up something in the original application folder.

Rails scaffold not working as expected [duplicate]

This question already has answers here:
Rails g migration doesn't work
(3 answers)
Closed 8 years ago.
I'm currently doing Michael Hartls Railstutorials and ran into a problem i don't quite grasp.
When i try to generate a User Scaffold with this command
benjamins-air-3:demo_app caspar$ rails generate scaffold User name:string email:string
I merely get a listing of the options for the rails command, such as this
benjamins-air-3:demo_app caspar$ rails generate scaffold User name:string email:string
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /Users/caspar/.rvm/rubies/ruby-2.0.0-p451/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], [--no-skip-gemfile] # Don't create a Gemfile
[--skip-bundle], [--no-skip-bundle] # Don't run bundle install
-G, [--skip-git], [--no-skip-git] # Skip Git ignores and keeps
-O, [--skip-active-record], [--no-skip-active-record] # Skip Active Record files
-S, [--skip-sprockets], [--no-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], [--no-skip-javascript] # Skip JavaScript files
[--dev], [--no-dev] # Setup the application with Gemfile pointing to your Rails checkout
[--edge], [--no-edge] # Setup the application with Gemfile pointing to Rails repository
-T, [--skip-test-unit], [--no-skip-test-unit] # Skip Test::Unit files
[--old-style-hash], [--no-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], [--no-pretend] # Run but do not make any changes
-q, [--quiet], [--no-quiet] # Suppress status output
-s, [--skip], [--no-skip] # Skip files that already exist
Rails options:
-h, [--help], [--no-help] # Show this help message and quit
-v, [--version], [--no-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.
benjamins-air-3:demo_app caspar$ ruby -v
ruby 2.0.0p451 (2014-02-24 revision 45167) [x86_64-darwin13.1.0]
benjamins-air-3:demo_app caspar$ rails -v
Rails 3.2.14
benjamins-air-3:demo_app caspar$
But no views or controllers are being generated. Any hints as to what I'm missing out on here? Thanks a lot
I suppose you've created rails new using one version of Rails and than changed it in the Gemfile. Because of that if you run any rails generate command it'll use new version but it'll not detect your directory as any project, so it'll not create anything.

Rails scaffolding not generating options menu appears

Here is step by step what I did.
cd ~/rails_projects
rails new demo_app1
cd demo_app1
(modified my gem file for rails 3.2.13. Ssqlite3 to 1.3.5 sass-rails to 3.2.4 coffee rails to 3.2.2 uglier to 1.2.3 jquery-rails to 2.0.1 and pg to 0.12.2)
then bundle update
bundle install --without production
git init
git add .
git commit -m "initial commit"
then rails generate scaffold User name:string email:string
but it doesn't generate the scaffolding all I get is the options menu which is posted below.
Chris-Schusters-MacBook-Pro:demo_app1 schust49$ rails generate scaffold user name:string email:string
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /Users/schust49/.rvm/rubies/ruby-1.9.3-p484/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.
Where am I going wrong and how do I fix this to make it scaffold?
First you have to install the `gem 'sqlite3' add that in gemfile
And then bundle install......
one more thing firstly if you have RVM insalled on your machine then select a ruby version with rvm and after that
check the ruby version
ruby -v
and rails version
rails -v
if both give you version which you have used in your application and bundle complete then it will not give the above options and create the model

Novice learning RoR: Trying to run Rails server but getting odd output in terminal

I'm trying to run Rails Server in Ubuntu 10.10 terminal. But I get an output completely different from that shown in my tutorial:
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /home/rinki/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
-d, [--database=DATABASE] # Preconfigure for selected database (options:
mysql/oracle /postgresql/sqlite3/frontbase/ibm_db)
# Default: sqlite3
-b, [--builder=BUILDER] # Path to an application builder (can be a filesystem path
or URL)
-m, [--template=TEMPLATE] # Path to an application template (can be a filesystem path
or URL)
[--dev] # Setup the application with Gemfile pointing to your Rails
checkout
[--edge] # Setup the application with Gemfile pointing to Rails
repository
[--skip-gemfile] # Don't create a Gemfile
-O, [--skip-active-record] # Skip Active Record files
-T, [--skip-test-unit] # Skip Test::Unit files
-J, [--skip-prototype] # Skip Prototype files
-G, [--skip-git] # Skip Git ignores and keeps
Runtime options:
-f, [--force] # Overwrite files that already exist
-p, [--pretend] # Run but do not make any changes
-q, [--quiet] # Supress status output
-s, [--skip] # Skip files that already exist
Rails options:
-v, [--version] # Show Rails version number and quit
-h, [--help] # Show this help message and quit
Description:
The 'rails new' command creates a new Rails application with a default
directory structure and configuration at the path you specify.
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.
Any help/suggestions would be much appreciated.
Thanks!
Have you created a project in the directory first? It looks like you haven't. Once you setup a project with rails new you will be able to run the rails server for the site.
If you want to run rails server type in your console "rails server", and if you want create a new project type in your console "rails new project_name"
What version of Rails are you running?
In your terminal type 'rails -v'. If you do not have version 3 then the 'rails new app_name' syntax will not work for this was changed in Rails 3.
Try updating Rails and trying again: 'gem update rails'.
This is the best documentation you can get for ubuntu on Rails. It will help you set up everything pretty easily :
https://help.ubuntu.com/community/RubyOnRails

Resources