Scaffoldで作られるもの

Scaffoldは使わないがそれで作られるソースを参考にしたいことがあったので、scaffoldでshopというmodelを作った場合のメモ

rails3.2.11を仕様 assets,testは省略

model

namestring
addressstring
phoneinteger

Controller shops_controller.rb

class ShopsController < ApplicationController
  # GET /shops
  # GET /shops.json
  def index
    @shops = Shop.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @shops }
    end
  end

  # GET /shops/1
  # GET /shops/1.json
  def show
    @shop = Shop.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @shop }
    end
  end

  # GET /shops/new
  # GET /shops/new.json
  def new
    @shop = Shop.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @shop }
    end
  end

  # GET /shops/1/edit
  def edit
    @shop = Shop.find(params[:id])
  end

  # POST /shops
  # POST /shops.json
  def create
    @shop = Shop.new(params[:shop])

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

  # PUT /shops/1
  # PUT /shops/1.json
  def update
    @shop = Shop.find(params[:id])

    respond_to do |format|
      if @shop.update_attributes(params[:shop])
        format.html { redirect_to @shop, notice: 'Shop was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @shop.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /shops/1
  # DELETE /shops/1.json
  def destroy
    @shop = Shop.find(params[:id])
    @shop.destroy

    respond_to do |format|
      format.html { redirect_to shops_url }
      format.json { head :no_content }
    end
  end
end

Views

index.html.erb

<h1>Listing shops</h1>

<table>
  <tr>
    <th>Name</th>
    <th>Address</th>
    <th>Phone</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @shops.each do |shop| %>
  <tr>
    <td><%= shop.name %></td>
    <td><%= shop.address %></td>
    <td><%= shop.phone %></td>
    <td><%= link_to 'Show', shop %></td>
    <td><%= link_to 'Edit', edit_shop_path(shop) %></td>
    <td><%= link_to 'Destroy', shop, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New Shop', new_shop_path %>

new.html.erb

<h1>New shop</h1>

<%= render 'form' %>

<%= link_to 'Back', shops_path %>

edit.html.erb

<h1>Editing shop</h1>

<%= render 'form' %>

<%= link_to 'Show', @shop %> |
<%= link_to 'Back', shops_path %>

show.html.erb

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

<p>
  <b>Name:</b>
  <%= @shop.name %>
</p>

<p>
  <b>Address:</b>
  <%= @shop.address %>
</p>

<p>
  <b>Phone:</b>
  <%= @shop.phone %>
</p>


<%= link_to 'Edit', edit_shop_path(@shop) %> |
<%= link_to 'Back', shops_path %>

_form.html.erb

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

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

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :address %><br />
    <%= f.text_field :address %>
  </div>
  <div class="field">
    <%= f.label :phone %><br />
    <%= f.number_field :phone %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Model shop.rb

class Shop < ActiveRecord::Base
  attr_accessible :address, :name, :phone
end
rubyonrails/other/scaffold_sample.txt · 最終更新: 2013/02/13 01:07 by enibloadmin
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0