From 50a9de514f8724b04d3681aa9ca228a8ca490909 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sun, 13 Nov 2011 03:15:56 -0700 Subject: Update guide to use Ruby 1.9 hash syntax --- railties/guides/source/getting_started.textile | 88 +++++++++++++------------- 1 file changed, 43 insertions(+), 45 deletions(-) (limited to 'railties/guides/source') diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 8faacdd03e..8f34e87e9a 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -524,10 +524,10 @@ Blog::Application.routes.draw do #... # You can have the root of your site routed with "root" # just remember to delete public/index.html. - root :to => "home#index" + root to: "home#index" -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. 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: class Post < ActiveRecord::Base - validates :name, :presence => true - validates :title, :presence => true, - :length => { :minimum => 5 } + validates :name, presence: true + validates :title, presence: true, + length: { minimum: 5 } end @@ -725,7 +725,7 @@ open a console that will roll back any changes you make by using rails conso After the console loads, you can use it to work with your application's models: ->> p = Post.new(:content => "A new post") +>> p = Post.new(content: "A new post") => # @@ -758,11 +758,15 @@ def index respond_to do |format| format.html # index.html.erb - format.json { render :json => @posts } + format.json { render json: @posts } end end +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 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+. @@ -797,8 +801,7 @@ Here's +app/views/posts/index.html.erb+: <%= post.content %> <%= link_to 'Show', post %> <%= link_to 'Edit', edit_post_path(post) %> - <%= link_to 'Destroy', post, :confirm => 'Are you sure?', - :method => :delete %> + <%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %> <% end %> @@ -866,7 +869,7 @@ def new respond_to do |format| format.html # new.html.erb - format.json { render :json => @post } + format.json { render json: @post } end end @@ -958,14 +961,11 @@ def create 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 } + 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 } + format.html { render action: "new" } + format.json { render json: @post.errors, status: :unprocessable_entity } end end end @@ -1003,8 +1003,8 @@ def show @post = Post.find(params[:id]) respond_to do |format| - format.html # show.html.erb - format.json { render :json => @post } + format.html # show.html.erb + format.json { render json: @post } end end @@ -1073,13 +1073,11 @@ def update respond_to do |format| if @post.update_attributes(params[:post]) - format.html { redirect_to(@post, - :notice => 'Post was successfully updated.') } - format.json { render :json => {}, :status => :ok } + format.html { redirect_to @post, notice: 'Post was successfully updated.' } + format.json { head :ok } else - format.html { render :action => "edit" } - format.json { render :json => @post.errors, - :status => :unprocessable_entity } + format.html { render action: "edit" } + format.json { render json: @post.errors, status: :unprocessable_entity } end end end @@ -1215,9 +1213,9 @@ You'll need to edit the +post.rb+ file to add the other side of the association: class Post < ActiveRecord::Base - validates :name, :presence => true - validates :title, :presence => true, - :length => { :minimum => 5 } + validates :name, presence: true + validates :title, presence: true, + length: { :minimum: 5 } has_many :comments end @@ -1559,8 +1557,8 @@ So first, let's add the delete link in the

<%= link_to 'Destroy Comment', [comment.post, comment], - :confirm => 'Are you sure?', - :method => :delete %> + confirm: 'Are you sure?', + method: :delete %>

@@ -1602,10 +1600,10 @@ model, +app/models/post.rb+, as follows: class Post < ActiveRecord::Base - validates :name, :presence => true - validates :title, :presence => true, - :length => { :minimum => 5 } - has_many :comments, :dependent => :destroy + validates :name, presence: true + validates :title, presence: true, + length: {minimum: 5} + has_many :comments, dependent: :destroy end @@ -1629,7 +1627,7 @@ action, except for +index+ and +show+, so we write that: 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.json @@ -1645,7 +1643,7 @@ We also only want to allow authenticated users to delete comments, so in the 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 @post = Post.find(params[:post_id]) @@ -1683,15 +1681,14 @@ edit tags via posts: class Post < ActiveRecord::Base - validates :name, :presence => true - validates :title, :presence => true, - :length => { :minimum => 5 } - - has_many :comments, :dependent => :destroy + 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? } } + accepts_nested_attributes_for :tags, allow_destroy: :true, + reject_if: proc { |attrs| attrs.all? { |k, v| v.blank? } } end @@ -1729,8 +1726,9 @@ We will modify +views/posts/_form.html.erb+ to render a partial to make a tag: <%= post_form.text_area :content %>

Tags

- <%= render :partial => 'tags/form', - :locals => {:form => post_form} %> + <%= render partial: 'tags/form', + locals: {form: post_form} %> +
<%= post_form.submit %>
-- cgit v1.2.3