forked from rails/rails
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt "Getting started guide" code sample
- Loading branch information
1 parent
0d5a7ad
commit f6d5036
Showing
8 changed files
with
36 additions
and
129 deletions.
There are no files selected for viewing
76 changes: 2 additions & 74 deletions
76
guides/code/getting_started/app/controllers/posts_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
45 changes: 14 additions & 31 deletions
45
guides/code/getting_started/app/views/posts/_form.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
<%= render 'form' %> | ||
<%= link_to 'Back', posts_path %> | ||
<%#= link_to 'Back', posts_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...db/migrate/20110901012504_create_posts.rb → ...db/migrate/20120420083127_create_posts.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |