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

    –skip-test-unit

    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.
    hello-world.html

    <html>
    <head>
    <title>Hello world!</title>
    </head>
    <body>
    <h1>HelloWorld</h1>
    </body>
    </html>
    

    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

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

    Layout

    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
      end
    end
    &#91;/ruby&#93;
    View example
    &#91;ruby&#93;
    Current time: <%=@current_time %>
    

    Partials

    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
    etc…

    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 %>
    

    More

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

    Redirect

    class RedirectController < ApplicationController
      def a
         redirect_to redirect_b_path
      end
    
      def b
      end
    end
    &#91;/ruby&#93;
    Please check rake routes
    <pre>
    redirect_a GET /redirect/a(.:format)
    redirect_b GET /redirect/b(.:format)
    </pre>
    Following is equivalent for it
    [ruby]
    redirect_to :action => 'b'
    

    Back

    Redirect the page you have just been

    redirect_to :back
    

    Helper

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