Scaffolding
Scaffolding realizes CRUD(Create, Read, Update, Delete) operations easily.
Scaffolding has special, and migration file is also created
by scaffold command
This is not practical way but, it is very useful to learn 😉
Create Scaffolding set
mode, controller, views, assets, etc…
Example)
rails g scaffold item title:string price:integer publish:string published:date
Use scaffold. Almost same as model definition.
Directory structure is following.
/application
/app
/models
item.rb : items table model class
/controllers
items_controller.rb : controller(CRUD)
/views
/items
index.html.erb : list
index.json.jbuilder : list(json)
edit.html.erb : edit
show.html.erb : show details
show.json.jbuilder : show details(json)
new.html.erb : create
_form.html.erb : create/edit common form
/assets
/javascripts
items.js.coffee : controller coffeescript
/stylesheets
items.css.scss : books controller SCSS
scaffolds.css.scss : SCSS for Scaffolding
/helpers
items_helper.rb : books controller helper
/db
/migrate
20141016145059_create_itemks.rb : migration file
/test
/models
item_test.rb : Model class test script
/helpers
items_helper_test.rb : View helper
/controllers
items_controller_test.rb : Controller test script
/fixtures
items.yml : Fixture file
Next step, we will do migration.
Do migration
rake db:migrate
(Already exist rake db:migrate:reset)
You can see list http://localhost:3000/items
Delete
rails destroy scaffold item
Route
config/routes.rb
resources :items
Check routes using command
rake routes
| Prefix | Verb | URL pattern | Controller#Action | Role |
|---|---|---|---|---|
| items | GET | /items(.:format) | items#index | List |
| POST | /items(.:format) | items#create | Register | |
| new_item | GET | /items?new(.:format) | items#new | Create form |
| edit_item | GET | /items/edit(.:format) | items#edit | Edit form |
| item | GET | /items/:id(.:format) | items#show | Detail |
| PATCH | /items/:id(.:format) | items#update | Update | |
| PUT | items/:id(.:format) | items#udpate | Update | |
| DELETE | items/:id(.:format) | items#destroy | Delete | |
| GET|POST|PATCH | /:controller(/:action(/:id))(.:format) | :controller#:action |
Actions and Views
All actions are already implemented by Rails
app/controllers/items_controller.rb
List(index) action
def index @items = Item.all end
View
index.json.jbuilder : JSON format
http://localhost:3000/items.json
items/index.html.erb
This is template
<h1>Listing items</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th>Publish</th>
<th>Published</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @items.each do |item| %>
<tr>
<td><%= item.title %></td>
<td><%= item.price %></td>
<td><%= item.publish %></td>
<td><%= item.published %></td>
<td><%= link_to 'Show', item %></td>
<td><%= link_to 'Edit', edit_book_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 %>
Some links are generated automatically.
link_to
<%= link_to 'Show', item %> # @item.id
The link is /items/1
ViewHelpers
| Helper | Path |
|---|---|
| items_path | /items |
| item_path(id) | /items/:id |
| new_item_path | /items/new |
| edit_item_path(id) | /items/:id/edit |
Show
Show each item details
app/controllers/items_controller.rb
before_action :set_item, only: [:show, :edit, :update, :destroy]
def show
end
private
def set_item
@item = Item.find(params[:id])
end
Retrieve data using id.
show.html.erb
<p id="notice"><%= notice %></p> <p> <strong>Title:</strong> <%= @item.title %> </p> <p> <strong>Price:</strong> <%= @item.price %> </p> <p> <strong>Publish:</strong> <%= @item.publish %> </p> <p> <strong>Published:</strong> <%= @item.published %> </p> <%= link_to 'Edit', edit_item_path(@item) %> | <%= link_to 'Back', items_path %>
notice tag : notice message place holder
new, create
Form
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 book from being saved:</h2>
<ul>
<% @item.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.number_field :price %>
</div>
<div class="field">
<%= f.label :publish %><br>
<%= f.text_field :publish %>
</div>
<div class="field">
<%= f.label :published %><br>
<%= f.date_select :published %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Actions
app/controllers/items_controller.rb
def new
@item = Item.new
end
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
Get post data
params.require(:item).permit(:title, :price, :publish)
edit/update
app/controllers/items_controller.rb
def edit
end
def update
respond_to do |format|
if @item.update(item_params)
format.html { redirect_to @item, notice: 'Item was successfully update.' }
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
Views
app/views/items/edit.html.erb
<h1>Editing item</h1> <%= render 'form' %> <%= link_to 'Show', @item %> <%= link_to 'Back', items_path %>
destroy
app/controllers/items_controller.rb
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
