Rails4.0.0.beta1 + Scaffold


```
ruby -v
----------
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.2.1]
----------
```

```
gem i rails -v 4.0.0.beta1
----------
Fetching: atomic-1.0.1.gem (100%)
Building native extensions.  This could take a while...
Successfully installed atomic-1.0.1
Fetching: thread_safe-0.1.0.gem (100%)
Successfully installed thread_safe-0.1.0
Fetching: activesupport-4.0.0.beta1.gem (100%)
Successfully installed activesupport-4.0.0.beta1
Fetching: builder-3.1.4.gem (100%)
Successfully installed builder-3.1.4
Fetching: rack-1.5.2.gem (100%)
Successfully installed rack-1.5.2
Fetching: actionpack-4.0.0.beta1.gem (100%)
Successfully installed actionpack-4.0.0.beta1
Fetching: activemodel-4.0.0.beta1.gem (100%)
Successfully installed activemodel-4.0.0.beta1
Fetching: arel-4.0.0.beta1.gem (100%)
Successfully installed arel-4.0.0.beta1
Fetching: activerecord-deprecated_finders-0.0.3.gem (100%)
Successfully installed activerecord-deprecated_finders-0.0.3
Fetching: activerecord-4.0.0.beta1.gem (100%)
Successfully installed activerecord-4.0.0.beta1
Fetching: mail-2.5.3.gem (100%)
Successfully installed mail-2.5.3
Fetching: actionmailer-4.0.0.beta1.gem (100%)
Successfully installed actionmailer-4.0.0.beta1
Fetching: railties-4.0.0.beta1.gem (100%)
Successfully installed railties-4.0.0.beta1
Fetching: bundler-1.3.0.gem (100%)
Successfully installed bundler-1.3.0
Fetching: sprockets-2.9.0.gem (100%)
Successfully installed sprockets-2.9.0
Fetching: sprockets-rails-2.0.0.rc3.gem (100%)
Successfully installed sprockets-rails-2.0.0.rc3
Fetching: rails-4.0.0.beta1.gem (100%)
Successfully installed rails-4.0.0.beta1
Done installing documentation for atomic, thread_safe, activesupport, builder, rack, actionpack, activemodel, arel, activerecord-deprecated_finders, activerecord, mail, actionmailer, railties, bundler, sprockets, sprockets-rails, rails (0 sec).
17 gems installed
----------
```

```
rails new rails4sample
----------
Using --skip-bundle --skip-test-unit from /Users/shu/.railsrc
      create  
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/images/rails.png
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/views/layouts/application.html.erb
      create  app/mailers/.keep
      create  app/models/.keep
      create  app/controllers/concerns/.keep
      create  app/models/concerns/.keep
      create  bin
      create  bin/bundle
      create  bin/rails
      create  bin/rake
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/robots.txt
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.keep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.keep
----------
```

```
cd rails4sample
```

```
bundle install
```

bundle installエラー => [bundle install - OpenSSL::SSL::SSLError #Ruby #Rails - Qiita](http://qiita.com/items/12c51a515f2dfac4e213)

```
rails g scaffold task title:string content:text deadline:timestamp count:integer
----------
      invoke  active_record
      create    db/migrate/20130227074921_create_tasks.rb
      create    app/models/task.rb
      invoke  resource_route
       route    resources :tasks
      invoke  jbuilder_scaffold_controller
      create    app/controllers/tasks_controller.rb
      invoke    erb
      create      app/views/tasks
      create      app/views/tasks/index.html.erb
      create      app/views/tasks/edit.html.erb
      create      app/views/tasks/show.html.erb
      create      app/views/tasks/new.html.erb
      create      app/views/tasks/_form.html.erb
      invoke    helper
      create      app/helpers/tasks_helper.rb
      invoke    jbuilder
       exist      app/views/tasks
      create      app/views/tasks/index.json.jbuilder
      create      app/views/tasks/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/tasks.js.coffee
      invoke    scss
      create      app/assets/stylesheets/tasks.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss
----------
```


* app/controllers/tasks_controller.rb

```rb
class TasksController < ApplicationController
  before_action :set_task, only: [:show, :edit, :update, :destroy]

  # GET /tasks
  # GET /tasks.json
  def index
    @tasks = Task.all
  end

  # GET /tasks/1
  # GET /tasks/1.json
  def show
  end

  # GET /tasks/new
  def new
    @task = Task.new
  end

  # GET /tasks/1/edit
  def edit
  end

  # POST /tasks
  # POST /tasks.json
  def create
    @task = Task.new(task_params)

    respond_to do |format|
      if @task.save
        format.html { redirect_to @task, notice: 'Task was successfully created.' }
        format.json { render action: 'show', status: :created, location: @task }
      else
        format.html { render action: 'new' }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /tasks/1
  # PATCH/PUT /tasks/1.json
  def update
    respond_to do |format|
      if @task.update(task_params)
        format.html { redirect_to @task, notice: 'Task was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /tasks/1
  # DELETE /tasks/1.json
  def destroy
    @task.destroy
    respond_to do |format|
      format.html { redirect_to tasks_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_task
      @task = Task.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def task_params
      params.require(:task).permit(:title, :content, :deadline, :count)
    end
end
```

* app/models/task.rb

```rb
class Task < ActiveRecord::Base
end
```

* app/views/tasks/index.html.erb

```erb
<h1>Listing tasks</h1>

<table>
  <thead>
    <tr>
      <th>Title</th>
      <th>Content</th>
      <th>Deadline</th>
      <th>Count</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>

  <tbody>
    <% @tasks.each do |task| %>
    <tr>
      <td><%= task.title %></td>
      <td><%= task.content %></td>
      <td><%= task.deadline %></td>
      <td><%= task.count %></td>
      <td><%= link_to 'Show', task %></td>
      <td><%= link_to 'Edit', edit_task_path(task) %></td>
      <td><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %></td>
    </tr>
    <% end %>
  </tbody>
</table>

<br />

<%= link_to 'New Task', new_task_path %>
```

* app/views/tasks/show.html.erb

```erb
<p id="notice"><%= notice %></p>

<p>
  <strong>Title:</strong>
  <%= @task.title %>
</p>

<p>
  <strong>Content:</strong>
  <%= @task.content %>
</p>

<p>
  <strong>Deadline:</strong>
  <%= @task.deadline %>
</p>

<p>
  <strong>Count:</strong>
  <%= @task.count %>
</p>


<%= link_to 'Edit', edit_task_path(@task) %> |
<%= link_to 'Back', tasks_path %>
```

* app/views/tasks/new.html.erb

```erb
<h1>New task</h1>

<%= render 'form' %>

<%= link_to 'Back', tasks_path %>
```

* app/views/tasks/edit.html.erb

```erb
<h1>Editing task</h1>

<%= render 'form' %>

<%= link_to 'Show', @task %> |
<%= link_to 'Back', tasks_path %>
```

* app/views/tasks/_form.html.erb

```erb
<%= form_for(@task) do |f| %>
  <% if @task.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@task.errors.count, "error") %> prohibited this task from being saved:</h2>

      <ul>
      <% @task.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="field">
    <%= f.label :deadline %><br />
    <%= f.datetime_select :deadline %>
  </div>
  <div class="field">
    <%= f.label :count %><br />
    <%= f.number_field :count %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
```

* app/views/tasks/index.json.jbuilder

```rb
json.array!(@tasks) do |task|
  json.extract! task, :title, :content, :deadline, :count
  json.url task_url(task, format: :json)
end
```

* app/views/tasks/show.json.jbuilder

```rb
json.extract! @task, :title, :content, :deadline, :count, :created_at, :updated_at
```

* app/assets/javascripts/application.js

```js
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
```

* app/assets/stylesheets/application.css

```css
/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the top of the
 * compiled file, but it's generally better to create a new file per style scope.
 *
 *= require_self
 *= require_tree .
 */
```

* app/assets/stylesheets/scaffolds.css.scss

```
body {
  background-color: #fff;
  color: #333;
  font-family: verdana, arial, helvetica, sans-serif;
  font-size: 13px;
  line-height: 18px;
}

p, ol, ul, td {
  font-family: verdana, arial, helvetica, sans-serif;
  font-size: 13px;
  line-height: 18px;
}

pre {
  background-color: #eee;
  padding: 10px;
  font-size: 11px;
}

a {
  color: #000;
  &:visited {
    color: #666;
  }
  &:hover {
    color: #fff;
    background-color: #000;
  }
}

div {
  &.field, &.actions {
    margin-bottom: 10px;
  }
}

#notice {
  color: green;
}

.field_with_errors {
  padding: 2px;
  background-color: red;
  display: table;
}

#error_explanation {
  width: 450px;
  border: 2px solid red;
  padding: 7px;
  padding-bottom: 0;
  margin-bottom: 20px;
  background-color: #f0f0f0;
  h2 {
    text-align: left;
    font-weight: bold;
    padding: 5px 5px 5px 15px;
    font-size: 12px;
    margin: -7px;
    margin-bottom: 0px;
    background-color: #c00;
    color: #fff;
  }
  ul li {
    font-size: 12px;
    list-style: square;
  }
}
```

* config/routes.rb

```rb
Rails4sample::Application.routes.draw do
  resources :tasks

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  # root to: 'welcome#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end
```

* Gemfile

```
source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0.beta1'

gem 'sqlite3'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 4.0.0.beta1'
  gem 'coffee-rails', '~> 4.0.0.beta1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', platforms: :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.0.1'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano', group: :development

# To use debugger
# gem 'debugger'
```