Skip to content
Permalink
Browse files

./script/rails g scaffold article title:text text:text

  • Loading branch information...
masutaka committed Apr 13, 2013
1 parent a73fa5c commit f4ad98c41877341c410f1ece50b83e459fe1a417
@@ -0,0 +1,3 @@
# 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://jashkenas.github.com/coffee-script/
@@ -0,0 +1,3 @@
// Place all the styles related to the articles controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,69 @@
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;
}
}
@@ -0,0 +1,83 @@
class ArticlesController < ApplicationController
# GET /articles
# GET /articles.json
def index
@articles = Article.all

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

# GET /articles/1
# GET /articles/1.json
def show
@article = Article.find(params[:id])

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

# GET /articles/new
# GET /articles/new.json
def new
@article = Article.new

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

# GET /articles/1/edit
def edit
@article = Article.find(params[:id])
end

# POST /articles
# POST /articles.json
def create
@article = Article.new(params[:article])

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

# PUT /articles/1
# PUT /articles/1.json
def update
@article = Article.find(params[:id])

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

# DELETE /articles/1
# DELETE /articles/1.json
def destroy
@article = Article.find(params[:id])
@article.destroy

respond_to do |format|
format.html { redirect_to articles_url }
format.json { head :no_content }
end
end
end
@@ -0,0 +1,2 @@
module ArticlesHelper
end
@@ -0,0 +1,3 @@
class Article < ActiveRecord::Base
attr_accessible :text, :title
end
@@ -0,0 +1,25 @@
<%= form_for(@article) do |f| %>
<% if @article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>

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

<div class="field">
<%= f.label :title %><br />
<%= f.text_area :title %>
</div>
<div class="field">
<%= f.label :text %><br />
<%= f.text_area :text %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
@@ -0,0 +1,6 @@
<h1>Editing article</h1>

<%= render 'form' %>

<%= link_to 'Show', @article %> |
<%= link_to 'Back', articles_path %>
@@ -0,0 +1,25 @@
<h1>Listing articles</h1>

<table>
<tr>
<th>Title</th>
<th>Text</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Article', new_article_path %>
@@ -0,0 +1,5 @@
<h1>New article</h1>

<%= render 'form' %>

<%= link_to 'Back', articles_path %>
@@ -0,0 +1,15 @@
<p id="notice"><%= notice %></p>

<p>
<b>Title:</b>
<%= @article.title %>
</p>

<p>
<b>Text:</b>
<%= @article.text %>
</p>


<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
@@ -1,4 +1,7 @@
Sample::Application.routes.draw do
resources :articles


# The priority is based upon order of creation:
# first created -> highest priority.

@@ -0,0 +1,10 @@
class CreateArticles < ActiveRecord::Migration
def change
create_table :articles do |t|
t.text :title
t.text :text

t.timestamps
end
end
end
@@ -0,0 +1,23 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130413185044) do

create_table "articles", :force => true do |t|
t.text "title"
t.text "text"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

end
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
title: MyText
text: MyText

two:
title: MyText
text: MyText
@@ -0,0 +1,49 @@
require 'test_helper'

class ArticlesControllerTest < ActionController::TestCase
setup do
@article = articles(:one)
end

test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:articles)
end

test "should get new" do
get :new
assert_response :success
end

test "should create article" do
assert_difference('Article.count') do
post :create, article: { text: @article.text, title: @article.title }
end

assert_redirected_to article_path(assigns(:article))
end

test "should show article" do
get :show, id: @article
assert_response :success
end

test "should get edit" do
get :edit, id: @article
assert_response :success
end

test "should update article" do
put :update, id: @article, article: { text: @article.text, title: @article.title }
assert_redirected_to article_path(assigns(:article))
end

test "should destroy article" do
assert_difference('Article.count', -1) do
delete :destroy, id: @article
end

assert_redirected_to articles_path
end
end
@@ -0,0 +1,7 @@
require 'test_helper'

class ArticleTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
@@ -0,0 +1,4 @@
require 'test_helper'

class ArticlesHelperTest < ActionView::TestCase
end

0 comments on commit f4ad98c

Please sign in to comment.
You can’t perform that action at this time.