Rails Basics Application
Create new application
rails new application -d mysql --skip-test-unit
Other options Rails Basics
Application Directory
/application /app /assets /images Image /javascripts JavaScript(CoffeeScript) /stylesheets CSS(SCSS) /controllers Controller class /concerns Controller common module application_controller.rb Application common controller /helpers View Helper application_heler.rb Application common helper /mailers ActionMailer /models Model class /views View script /layout layout application.html.erb Application common layout /bin Helper script /config Config /environments /initializers /locales /db /lib Library by yourself /assets /log /public /test /tmp /vendor 3rd party codes /assets 3rd party assets config.ru Application entry point Gemfile Rakefile README.rdoc
Controller
Details are Rails Controller
Controllers and Action are Rails Controller Action
rails g controller hello
What is created?
/application /app /assets /javascripts hello.js.coffee (coffee) /stylesheets hello.css.scss (scss) /controllers hello_controller.rb /views /hello (erb) /helpers hello_helpers.rb (helper) /test /controllers hello_controller_test.rb (test_unit) /helpers hello_helper_test.rb (test_unit)
Clean controller
rails destroy controller controllername
Class
class HelloController < ApplicationController end [/ruby] Output text [ruby] render text: '' [/ruby] <h4>Files naming rule</h4> [bash] rails g controller abc [/bash] <table> <tr><th>Name</th><th>File name</th></tr> <tr><td>class</td><td>ABCController</td></tr> <tr><td>file</td><td>abc_controller.rb</td></tr> <tr><td>helper</td><td>abc_helper.rb</td></tr> <tr><td>test</td><td>abc_controller_test.rb</td></tr> </table> <hr> <h3>Basic Route</h3> config/routes.rb [ruby] action ':controller(/:action(:id))', via: [ :get, :post, :patch ]
This covers all controller/action (not for production)
Example) /hello/index
View
ERB
app/views/hello/index.html.erb
app/views/controllername/actionname.html.erb
<% %> <%= @value %>
Model Basics
Details are following
Model Start Up
database setting is config/database.yml
Details are Database and check
Create model
rails g model book
Several files are created
/application /app /models book.rb Model /db /migrate 20141012132619_create_book.rb /test /models book_test.rb /fixtures book.yml Fixture files for test
migration file is in db/migrate
Add type
Add db column in migration file
class CreateBooks < ActiveRecord::Migration def change create_table :books do |t| t.string :isbn t.string :title t.integer :price t.string :publish t.date :published t.boolean :cd t.timestamps end end end [/ruby] <h4>migration</h4> [bash] rake db:migrate [/bash] <h4>Check database</h4> We can check database using dbconsole [bash] rails dbconsole [/bash] Open clinent using config/database.yml In case of Sqlite3, open sqlite3 [bash] sqlite>.tables sqlite>.schema tablename sqlite>.quit [/bash] <hr> <h3>Simple Action and View</h3> <h4>List Action</h4> app/controllers/hello_controller.rb [ruby] def list @books = Book.all end
View template
app/views/hello/list.html.erb
<table border="1"> <tr> <th>Name</th><th>Price</th> </tr> <%= @books.each do |book| %> <tr> <td><%= book.isbn %></td> <td><%= book.title %></td> <td><%= book.price %></td> </tr> <% end %> </table>