RailsでScaffoldを使ってみる(続き)
生成されたコードを確認してみる。
active_record
db/migrate/20141013120655_create_items.rb
class createitems < activerecord::migration def change create_table :items do |t| t.string :name t.integer :price t.timestamps end end end
app/models/item.rb
class item < activerecord::base end
resource_route
config/routes.rb
rails.application.routes.draw do resources :items ・・・
scaffold_controller
app/controllers/items_controller.rb
class itemscontroller < applicationcontroller before_action :set_item, only: [:show, :edit, :update, :destroy] # get /items # get /items.json def index @items = item.all end # get /items/1 # get /items/1.json def show end # get /items/new def new @item = item.new end # get /items/1/edit def edit end # post /items # post /items.json def create @item = item.new(item_params) respond_to do |format| if @item.save format.html { redirect_to @item, notice: 'item was successfully created.' } format.json { render :show, status: :created, location: @item } else format.html { render :new } format.json { render json: @item.errors, status: :unprocessable_entity } end end end # patch/put /items/1 # patch/put /items/1.json def update respond_to do |format| if @item.update(item_params) format.html { redirect_to @item, notice: 'item was successfully updated.' } format.json { render :show, status: :ok, location: @item } else format.html { render :edit } format.json { render json: @item.errors, status: :unprocessable_entity } end end end # delete /items/1 # delete /items/1.json def destroy @item.destroy respond_to do |format| format.html { redirect_to items_url, notice: 'item was successfully destroyed.' } format.json { head :no_content } end end private # use callbacks to share common setup or constraints between actions. def set_item @item = item.find(params[:id]) end # never trust parameters from the scary internet, only allow the white list through. def item_params params.require(:item).permit(:name, :price) end end
scaffold_controller(erb)
app/views/items/index.html.erb
<h1>listing items</h1> <table> <thead> <tr> <th>name</th> <th>price</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @items.each do |item| %> <tr> <td><%= item.name %></td> <td><%= item.price %></td> <td><%= link_to 'show', item %></td> <td><%= link_to 'edit', edit_item_path(item) %></td> <td><%= link_to 'destroy', item, method: :delete, data: { confirm: 'are you sure?' } %></td> </tr> <% end %> </tbody> </table> <br> <%= link_to 'new item', new_item_path %>
app/views/items/edit.html.erb
<h1>editing item</h1> <%= render 'form' %> <%= link_to 'show', @item %> | <%= link_to 'back', items_path %>
cat app/views/items/show.html.erb
<p id="notice"><%= notice %></p> <p> <strong>name:</strong> <%= @item.name %> </p> <p> <strong>price:</strong> <%= @item.price %> </p> <%= link_to 'edit', edit_item_path(@item) %> | <%= link_to 'back', items_path %>
app/views/items/new.html.erb
<h1>new item</h1> <%= render 'form' %> <%= link_to 'back', items_path %>
app/views/items/_form.html.erb
<%= form_for(@item) do |f| %> <% if @item.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@item.errors.count, "error") %> prohibited this item from being saved:</h2> <ul> <% @item.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :name %><br> <%= f.text_field :name %> </div> <div class="field"> <%= f.label :price %><br> <%= f.number_field :price %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
scaffold_controller(helper)
app/helpers/items_helper.rb
module itemshelper end
scaffold_controller(jbuilder)
assets
app/assets/javascripts/items.js.coffee
# place all the behaviors and hooks related to the matching controller here. # all this logic will automatically be available in application.js. # you can use coffeescript in this file: http://coffeescript.org/
app/assets/stylesheets/items.css.scss
// place all the styles related to the items controller here. // they will automatically be included in application.css. // you can use sass (scss) here: http://sass-lang.com/
scss
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; } }