Rails Basics

Basics Topics

– DRY Don’t repeat yourself
– Refactoring

  • Create new Application
  • Create new Application with MySQL
  • Create new Application without Database
  • Create new application without bundle install
  • Create Application without UnitTest
  • Start Application
  • Stop Application
  • Rails

    Create new Application

    Before it, you should create workspace and move there.

    rails new applicationame

    Create new Application with MySQL

    rails new applicationname -d mysql

    Create new application without bundle install

    To fix dependency my myself

    ruby new applicationname --skip-bundle

    Create new Application without Database

    Not create Active Record file

    rails new 

    Create Application without UnitTest


    rails new project -d mysql --skip-test-unit

    Start Application

    Move to application root folder, and start WEBrick server.

    rails s

    Stop Application

    Ctrl + C

    Static file

    You can static file(.html etc..) under applicationroot/public/xxx
    This is testing.

    <title>Hello world!</title>

    Please access http://localhost:3000/hello-world.html
    You can see web page contents of hello-world.html

    Create HTML dynamically with erb(controller)

    Let’s create a controller

    rails generate controller Example test

    The following files are created
    rails generate files
    Views are under app/views
    example/test.html.erb was created.

    Start application and access http://localhost:3000/example/test
    You can see this file.
    Controllers are under app/controllers/
    The name is example_controller.rb

    Route Check

    Rails knows that for the URL path. The current routings can be listed wit the command

    rake routes

    Insert Ruby code in erb

    Use <%= %> This is output. If missing =, this is only Ruby code

    <%= 1 + 1 %>
    <p>Find me in app/views/example/test.html.erb</p>


    app/views/layouts/application.html.erb is common layout
    In this file, there is following code

    <%= yield %>

    This part is a part to insert each html.erb

    Passing Instance Variables from a Controller to View

    Use instance variable
    controller example

    class ExampleController < ApplicationController
      def test
         @current_time = Time.now
    View example
    Current time: <%=@current_time %>


    Insert page in .erb. The file name must start with an underscore _
    How to insert?

    <%= render "footer" %>

    _footer.html.erb is file name The location is same directory in this case.
    To write path correctly, you can use partials in any places, for example, under app/views/layouts

    How to pass parameter to Partials

    Use symbol

    <%= render "footer", :start_year => '2000' %>

    This is also fine. This code checks existance

    <%= "#{start_year}-" if defined? start_year %>


    <%= render :partial=>"footer",:locals => {:start_year => '2000'} %>


    class RedirectController < ApplicationController
      def a
         redirect_to redirect_b_path
      def b
    Please check rake routes
    redirect_a GET /redirect/a(.:format)
    redirect_b GET /redirect/b(.:format)
    Following is equivalent for it
    redirect_to :action => 'b'


    Redirect the page you have just been

    redirect_to :back


    Common task in a view.
    It must be under app/helpers