Adapt "Getting started guide" code sample
This commit is contained in:
parent
0d5a7ad84d
commit
f6d5036dd6
@ -1,84 +1,12 @@
|
||||
class PostsController < ApplicationController
|
||||
http_basic_authenticate_with :name => "dhh", :password => "secret", :except => :index
|
||||
# GET /posts
|
||||
# GET /posts.json
|
||||
def index
|
||||
@posts = Post.all
|
||||
|
||||
respond_to do |format|
|
||||
format.html # index.html.erb
|
||||
format.json { render json: @posts }
|
||||
end
|
||||
end
|
||||
|
||||
# GET /posts/1
|
||||
# GET /posts/1.json
|
||||
def show
|
||||
@post = Post.find(params[:id])
|
||||
|
||||
respond_to do |format|
|
||||
format.html # show.html.erb
|
||||
format.json { render json: @post }
|
||||
end
|
||||
end
|
||||
|
||||
# GET /posts/new
|
||||
# GET /posts/new.json
|
||||
def new
|
||||
@post = Post.new
|
||||
|
||||
respond_to do |format|
|
||||
format.html # new.html.erb
|
||||
format.json { render json: @post }
|
||||
end
|
||||
end
|
||||
|
||||
# GET /posts/1/edit
|
||||
def edit
|
||||
@post = Post.find(params[:id])
|
||||
end
|
||||
|
||||
# POST /posts
|
||||
# POST /posts.json
|
||||
def create
|
||||
@post = Post.new(params[:post])
|
||||
|
||||
respond_to do |format|
|
||||
if @post.save
|
||||
format.html { redirect_to @post, notice: 'Post was successfully created.' }
|
||||
format.json { render json: @post, status: :created, location: @post }
|
||||
else
|
||||
format.html { render action: "new" }
|
||||
format.json { render json: @post.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# PUT /posts/1
|
||||
# PUT /posts/1.json
|
||||
def update
|
||||
@post = Post.find(params[:id])
|
||||
|
||||
respond_to do |format|
|
||||
if @post.update_attributes(params[:post])
|
||||
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
|
||||
format.json { head :no_content }
|
||||
else
|
||||
format.html { render action: "edit" }
|
||||
format.json { render json: @post.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /posts/1
|
||||
# DELETE /posts/1.json
|
||||
def destroy
|
||||
@post = Post.find(params[:id])
|
||||
@post.destroy
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to posts_url }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
@post.save
|
||||
redirect_to :action => :index
|
||||
end
|
||||
end
|
||||
|
@ -1,11 +1,10 @@
|
||||
class Post < ActiveRecord::Base
|
||||
validates :name, :presence => true
|
||||
validates :title, :presence => true,
|
||||
:length => { :minimum => 5 }
|
||||
|
||||
|
||||
has_many :comments, :dependent => :destroy
|
||||
has_many :tags
|
||||
|
||||
|
||||
accepts_nested_attributes_for :tags, :allow_destroy => :true,
|
||||
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
|
||||
end
|
||||
|
@ -1,32 +1,15 @@
|
||||
<% @post.tags.build %>
|
||||
<%= form_for(@post) do |post_form| %>
|
||||
<% if @post.errors.any? %>
|
||||
<div id="errorExplanation">
|
||||
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
|
||||
<ul>
|
||||
<% @post.errors.full_messages.each do |msg| %>
|
||||
<li><%= msg %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="field">
|
||||
<%= post_form.label :name %><br />
|
||||
<%= post_form.text_field :name %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= post_form.label :title %><br />
|
||||
<%= post_form.text_field :title %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= post_form.label :content %><br />
|
||||
<%= post_form.text_area :content %>
|
||||
</div>
|
||||
<h2>Tags</h2>
|
||||
<%= render :partial => 'tags/form',
|
||||
:locals => {:form => post_form} %>
|
||||
<div class="actions">
|
||||
<%= post_form.submit %>
|
||||
</div>
|
||||
<%= form_for :post, :url => { :action => :create } do |f| %>
|
||||
<p>
|
||||
<%= f.label :title %><br>
|
||||
<%= f.text_field :title %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= f.label :text %><br>
|
||||
<%= f.text_area :text %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= f.submit %>
|
||||
</p>
|
||||
<% end %>
|
||||
|
@ -2,4 +2,4 @@
|
||||
|
||||
<%= render 'form' %>
|
||||
|
||||
<%= link_to 'Back', posts_path %>
|
||||
<%#= link_to 'Back', posts_path %>
|
||||
|
@ -1,9 +1,10 @@
|
||||
Blog::Application.routes.draw do
|
||||
resources :posts do
|
||||
resources :comments
|
||||
end
|
||||
# resources :posts do
|
||||
# resources :comments
|
||||
# end
|
||||
|
||||
get "home/index"
|
||||
get "posts/new"
|
||||
post "posts/create"
|
||||
|
||||
# The priority is based upon order of creation:
|
||||
# first created -> highest priority.
|
||||
@ -55,7 +56,7 @@
|
||||
# You can have the root of your site routed with "root"
|
||||
# just remember to delete public/index.html.
|
||||
root :to => "home#index"
|
||||
|
||||
|
||||
# See how all your routes lay out with "rake routes"
|
||||
|
||||
# This is a legacy wild controller route that's not recommended for RESTful applications.
|
||||
|
@ -1,9 +1,8 @@
|
||||
class CreatePosts < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :posts do |t|
|
||||
t.string :name
|
||||
t.string :title
|
||||
t.text :content
|
||||
t.text :text
|
||||
|
||||
t.timestamps
|
||||
end
|
@ -11,31 +11,30 @@
|
||||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20110901013701) do
|
||||
ActiveRecord::Schema.define(:version => 20120420083127) do
|
||||
|
||||
create_table "comments", :force => true do |t|
|
||||
t.string "commenter"
|
||||
t.text "body"
|
||||
t.integer "post_id"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.datetime "created_at", :null => false
|
||||
t.datetime "updated_at", :null => false
|
||||
end
|
||||
|
||||
add_index "comments", ["post_id"], :name => "index_comments_on_post_id"
|
||||
|
||||
create_table "posts", :force => true do |t|
|
||||
t.string "name"
|
||||
t.string "title"
|
||||
t.text "content"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.text "text"
|
||||
t.datetime "created_at", :null => false
|
||||
t.datetime "updated_at", :null => false
|
||||
end
|
||||
|
||||
create_table "tags", :force => true do |t|
|
||||
t.string "name"
|
||||
t.integer "post_id"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.datetime "created_at", :null => false
|
||||
t.datetime "updated_at", :null => false
|
||||
end
|
||||
|
||||
add_index "tags", ["post_id"], :name => "index_tags_on_post_id"
|
||||
|
@ -1,11 +1,9 @@
|
||||
# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
|
||||
|
||||
one:
|
||||
name: MyString
|
||||
title: MyString
|
||||
content: MyText
|
||||
text: MyText
|
||||
|
||||
two:
|
||||
name: MyString
|
||||
title: MyString
|
||||
content: MyText
|
||||
text: MyText
|
||||
|
Loading…
Reference in New Issue
Block a user