mirror of https://github.com/rails/rails
Revert "Update guide to use Ruby 1.9 hash syntax"
This reverts commit 50a9de514f
.
Reason: Let's keep the guides at 1.8 syntax
This commit is contained in:
parent
50a9de514f
commit
e1099eb4fd
|
@ -524,10 +524,10 @@ Blog::Application.routes.draw do
|
||||||
#...
|
#...
|
||||||
# You can have the root of your site routed with "root"
|
# You can have the root of your site routed with "root"
|
||||||
# just remember to delete public/index.html.
|
# just remember to delete public/index.html.
|
||||||
root to: "home#index"
|
root :to => "home#index"
|
||||||
</ruby>
|
</ruby>
|
||||||
|
|
||||||
The +root to: "home#index"+ tells Rails to map the root action to the home
|
The +root :to => "home#index"+ tells Rails to map the root action to the home
|
||||||
controller's index action.
|
controller's index action.
|
||||||
|
|
||||||
Now if you navigate to "http://localhost:3000":http://localhost:3000 in your
|
Now if you navigate to "http://localhost:3000":http://localhost:3000 in your
|
||||||
|
@ -696,9 +696,9 @@ Open the +app/models/post.rb+ file and edit it:
|
||||||
|
|
||||||
<ruby>
|
<ruby>
|
||||||
class Post < ActiveRecord::Base
|
class Post < ActiveRecord::Base
|
||||||
validates :name, presence: true
|
validates :name, :presence => true
|
||||||
validates :title, presence: true,
|
validates :title, :presence => true,
|
||||||
length: { minimum: 5 }
|
:length => { :minimum => 5 }
|
||||||
end
|
end
|
||||||
</ruby>
|
</ruby>
|
||||||
|
|
||||||
|
@ -725,7 +725,7 @@ open a console that will roll back any changes you make by using <tt>rails conso
|
||||||
After the console loads, you can use it to work with your application's models:
|
After the console loads, you can use it to work with your application's models:
|
||||||
|
|
||||||
<shell>
|
<shell>
|
||||||
>> p = Post.new(content: "A new post")
|
>> p = Post.new(:content => "A new post")
|
||||||
=> #<Post id: nil, name: nil, title: nil,
|
=> #<Post id: nil, name: nil, title: nil,
|
||||||
content: "A new post", created_at: nil,
|
content: "A new post", created_at: nil,
|
||||||
updated_at: nil>
|
updated_at: nil>
|
||||||
|
@ -758,15 +758,11 @@ def index
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html # index.html.erb
|
format.html # index.html.erb
|
||||||
format.json { render json: @posts }
|
format.json { render :json => @posts }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
</ruby>
|
</ruby>
|
||||||
|
|
||||||
TIP: This Guide was written using Ruby 1.9.2. If you are using Ruby 1.8.7, some of
|
|
||||||
the syntax may look slightly different. For example, the hash syntax changed from
|
|
||||||
render :json => @posts (1.8.7) to render json: @posts (1.9.2).
|
|
||||||
|
|
||||||
+Post.all+ calls the all method on the +Post+ model, which returns all of
|
+Post.all+ calls the all method on the +Post+ model, which returns all of
|
||||||
the posts currently in the database. The result of this call is an array
|
the posts currently in the database. The result of this call is an array
|
||||||
of Post records that we store in an instance variable called +@posts+.
|
of Post records that we store in an instance variable called +@posts+.
|
||||||
|
@ -801,7 +797,8 @@ Here's +app/views/posts/index.html.erb+:
|
||||||
<td><%= post.content %></td>
|
<td><%= post.content %></td>
|
||||||
<td><%= link_to 'Show', post %></td>
|
<td><%= link_to 'Show', post %></td>
|
||||||
<td><%= link_to 'Edit', edit_post_path(post) %></td>
|
<td><%= link_to 'Edit', edit_post_path(post) %></td>
|
||||||
<td><%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %></td>
|
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',
|
||||||
|
:method => :delete %></td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
</table>
|
</table>
|
||||||
|
@ -869,7 +866,7 @@ def new
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html # new.html.erb
|
format.html # new.html.erb
|
||||||
format.json { render json: @post }
|
format.json { render :json => @post }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
</ruby>
|
</ruby>
|
||||||
|
@ -961,11 +958,14 @@ def create
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
if @post.save
|
if @post.save
|
||||||
format.html { redirect_to @post, notice: 'Post was successfully created.' }
|
format.html { redirect_to(@post,
|
||||||
format.json { render json: @post, status: :created, location: @post }
|
:notice => 'Post was successfully created.') }
|
||||||
|
format.json { render :json => @post,
|
||||||
|
:status => :created, :location => @post }
|
||||||
else
|
else
|
||||||
format.html { render action: "new" }
|
format.html { render :action => "new" }
|
||||||
format.json { render json: @post.errors, status: :unprocessable_entity }
|
format.json { render :json => @post.errors,
|
||||||
|
:status => :unprocessable_entity }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1003,8 +1003,8 @@ def show
|
||||||
@post = Post.find(params[:id])
|
@post = Post.find(params[:id])
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html # show.html.erb
|
format.html # show.html.erb
|
||||||
format.json { render json: @post }
|
format.json { render :json => @post }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
</ruby>
|
</ruby>
|
||||||
|
@ -1073,11 +1073,13 @@ def update
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
if @post.update_attributes(params[:post])
|
if @post.update_attributes(params[:post])
|
||||||
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
|
format.html { redirect_to(@post,
|
||||||
format.json { head :ok }
|
:notice => 'Post was successfully updated.') }
|
||||||
|
format.json { render :json => {}, :status => :ok }
|
||||||
else
|
else
|
||||||
format.html { render action: "edit" }
|
format.html { render :action => "edit" }
|
||||||
format.json { render json: @post.errors, status: :unprocessable_entity }
|
format.json { render :json => @post.errors,
|
||||||
|
:status => :unprocessable_entity }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1213,9 +1215,9 @@ You'll need to edit the +post.rb+ file to add the other side of the association:
|
||||||
|
|
||||||
<ruby>
|
<ruby>
|
||||||
class Post < ActiveRecord::Base
|
class Post < ActiveRecord::Base
|
||||||
validates :name, presence: true
|
validates :name, :presence => true
|
||||||
validates :title, presence: true,
|
validates :title, :presence => true,
|
||||||
length: { :minimum: 5 }
|
:length => { :minimum => 5 }
|
||||||
|
|
||||||
has_many :comments
|
has_many :comments
|
||||||
end
|
end
|
||||||
|
@ -1557,8 +1559,8 @@ So first, let's add the delete link in the
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<%= link_to 'Destroy Comment', [comment.post, comment],
|
<%= link_to 'Destroy Comment', [comment.post, comment],
|
||||||
confirm: 'Are you sure?',
|
:confirm => 'Are you sure?',
|
||||||
method: :delete %>
|
:method => :delete %>
|
||||||
</p>
|
</p>
|
||||||
</erb>
|
</erb>
|
||||||
|
|
||||||
|
@ -1600,10 +1602,10 @@ model, +app/models/post.rb+, as follows:
|
||||||
|
|
||||||
<ruby>
|
<ruby>
|
||||||
class Post < ActiveRecord::Base
|
class Post < ActiveRecord::Base
|
||||||
validates :name, presence: true
|
validates :name, :presence => true
|
||||||
validates :title, presence: true,
|
validates :title, :presence => true,
|
||||||
length: {minimum: 5}
|
:length => { :minimum => 5 }
|
||||||
has_many :comments, dependent: :destroy
|
has_many :comments, :dependent => :destroy
|
||||||
end
|
end
|
||||||
</ruby>
|
</ruby>
|
||||||
|
|
||||||
|
@ -1627,7 +1629,7 @@ action, except for +index+ and +show+, so we write that:
|
||||||
<ruby>
|
<ruby>
|
||||||
class PostsController < ApplicationController
|
class PostsController < ApplicationController
|
||||||
|
|
||||||
http_basic_authenticate_with name: "dhh", password: "secret", except: [:index, :show]
|
http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index, :show]
|
||||||
|
|
||||||
# GET /posts
|
# GET /posts
|
||||||
# GET /posts.json
|
# GET /posts.json
|
||||||
|
@ -1643,7 +1645,7 @@ We also only want to allow authenticated users to delete comments, so in the
|
||||||
<ruby>
|
<ruby>
|
||||||
class CommentsController < ApplicationController
|
class CommentsController < ApplicationController
|
||||||
|
|
||||||
http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy
|
http_basic_authenticate_with :name => "dhh", :password => "secret", :only => :destroy
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@post = Post.find(params[:post_id])
|
@post = Post.find(params[:post_id])
|
||||||
|
@ -1681,14 +1683,15 @@ edit tags via posts:
|
||||||
|
|
||||||
<ruby>
|
<ruby>
|
||||||
class Post < ActiveRecord::Base
|
class Post < ActiveRecord::Base
|
||||||
validates :name, presence: true
|
validates :name, :presence => true
|
||||||
validates :title, presence: true,
|
validates :title, :presence => true,
|
||||||
length: {minimum: 5}
|
:length => { :minimum => 5 }
|
||||||
has_many :comments, dependent: :destroy
|
|
||||||
|
has_many :comments, :dependent => :destroy
|
||||||
has_many :tags
|
has_many :tags
|
||||||
|
|
||||||
accepts_nested_attributes_for :tags, allow_destroy: :true,
|
accepts_nested_attributes_for :tags, :allow_destroy => :true,
|
||||||
reject_if: proc { |attrs| attrs.all? { |k, v| v.blank? } }
|
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
|
||||||
end
|
end
|
||||||
</ruby>
|
</ruby>
|
||||||
|
|
||||||
|
@ -1726,9 +1729,8 @@ We will modify +views/posts/_form.html.erb+ to render a partial to make a tag:
|
||||||
<%= post_form.text_area :content %>
|
<%= post_form.text_area :content %>
|
||||||
</div>
|
</div>
|
||||||
<h2>Tags</h2>
|
<h2>Tags</h2>
|
||||||
<%= render partial: 'tags/form',
|
<%= render :partial => 'tags/form',
|
||||||
locals: {form: post_form} %>
|
:locals => {:form => post_form} %>
|
||||||
|
|
||||||
<div class="actions">
|
<div class="actions">
|
||||||
<%= post_form.submit %>
|
<%= post_form.submit %>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue