How can I create sample application using Ruby on Rails?
Below are the steps to create a sample application:
- In the NetBeans IDE, choose File > New Project.
- Select Ruby in the Categories field and Ruby on Rails Application in the Projects field. Click Next.
- Type sampleapplication in the Project Name field.
- Select Ruby as a platform in place of JRuby in Ruby Platform field. Accept all the other default settings.
- Click Finish to create the new project. You will see a list of files in the project directory and database.yml file in the editing area.
- Create a sample database and edit the database.yml by providing the database information in the development configuration.
- Save the database.yml file.
Creating Model, View, Controller and migration script for user management:
- Go to the project folder from command prompt and run following command:
ruby script/generate scaffold user name:string email:string address:text phone:string
It will create model, views, controller and migration script in respective directory in your project and below is the output for the command:
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/users
exists app/views/layouts/
exists test/functional/
exists test/unit/
create test/unit/helpers/
exists public/stylesheets/
create app/views/users/index.html.erb
create app/views/users/show.html.erb
create app/views/users/new.html.erb
create app/views/users/edit.html.erb
create app/views/layouts/users.html.erb
create public/stylesheets/scaffold.css
create app/controllers/users_controller.rb
create test/functional/users_controller_test.rb
create app/helpers/users_helper.rb
create test/unit/helpers/users_helper_test.rb
route map.resources :users
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/user.rb
create test/unit/user_test.rb
create test/fixtures/users.yml
create db/migrate
create db/migrate/20090504184109_create_users.rb - In the Projects window, right-click the sampleapplication node and choose
Migrate Database > To Current Version.
Notes: If it generates error then run gem install mysql command from command prompt and copy libmySQL.dll from C:Program FilesMySQLMySQL Server 5.0bin to C:rubybin.
Run and test the Application:
- Open routes.rb under configuration node of project folder.
- Edit following line by removing # and change welcome to users.
# map.connect '', :controller => "welcome" or
# map.root :controller => "welcome" - Delete index.html under public node which is used to show default welcome page. Rails first look to this file then look to routes.rb to figure out what page to display.
- In the Projects window, right-click the sampleapplication node and click to Run or you can run project from command prompt by following command under project:
ruby scriptserver
Project creation has been completed and you can browse it in http://127.0.0.1:3000
Now you can create, edit, show and delete user by clicking respective link.
Validating inputs:
If you want to ensure users must provide values for both name and email fields, add following lines to User class under app/model folder of your project:
class User < ActiveRecord::Base
validates_presence_of :name, :email
validates_format_of :email, :with => /^([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/, :message => "Invalid"
end
Now system will give error message if you try to create user without name and email.
OK, Finally we have completed the sample application with data validation. To continue development using Ruby on Rails and skills up you will get lots of resources in web.
Click here for some good resources for Web development in Ruby on Rails
Nice reference for newbie.
Thanks for your nice post with some helpful resource link.