From c55ee776232f77a9bd45684a3e6f3f3b13380e4c Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Wed, 2 May 2012 11:39:36 +0200 Subject: Remove tags from getting started guide and adapt some of the content to the new guide --- guides/source/getting_started.textile | 216 +--------------------------------- 1 file changed, 2 insertions(+), 214 deletions(-) (limited to 'guides/source') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 8ea7c5ab6e..1fbbd8af67 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1540,12 +1540,12 @@ So first, let's add the delete link in the

- Commenter: + Commenter: <%= comment.commenter %>

- Comment: + Comment: <%= comment.body %>

@@ -1594,7 +1594,6 @@ 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 @@ -1623,11 +1622,8 @@ class PostsController < ApplicationController http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index, :show] - # GET /posts - # GET /posts.json def index @posts = Post.all - respond_to do |format| # snipped for brevity @@ -1649,214 +1645,6 @@ Authentication challenge !images/challenge.png(Basic HTTP Authentication Challenge)! -h3. Building a Multi-Model Form - -Another feature of your average blog is the ability to tag posts. To implement -this feature your application needs to interact with more than one model on a -single form. Rails offers support for nested forms. - -To demonstrate this, we will add support for giving each post multiple tags, -right in the form where you create the post. First, create a new model to hold -the tags: - - -$ rails generate model Tag name:string post:references - - -Again, run the migration to create the database table: - - -$ rake db:migrate - - -Next, edit the +post.rb+ file to create the other side of the association, and -to tell Rails (via the +accepts_nested_attributes_for+ macro) that you intend to -edit tags via posts: - - -class Post < ActiveRecord::Base - validates :name, :presence => true - validates :title, :presence => true, - :length => { :minimum => 5 } - - has_many :comments, :dependent => :destroy - has_many :tags - attr_protected :tags - - accepts_nested_attributes_for :tags, :allow_destroy => :true, - :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } -end - - -The +:allow_destroy+ option tells Rails to enable destroying tags through the -nested attributes (you'll handle that by displaying a "remove" checkbox on the -view that you'll build shortly). The +:reject_if+ option prevents saving new -tags that do not have any attributes filled in. - -We will modify +views/posts/_form.html.erb+ to render a partial to make a tag: - - -<% @post.tags.build %> -<%= form_for(@post) do |post_form| %> - <% if @post.errors.any? %> -
-

<%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:

-
    - <% @post.errors.full_messages.each do |msg| %> -
  • <%= msg %>
  • - <% end %> -
-
- <% end %> - -
- <%= post_form.label :name %>
- <%= post_form.text_field :name %> -
-
- <%= post_form.label :title %>
- <%= post_form.text_field :title %> -
-
- <%= post_form.label :content %>
- <%= post_form.text_area :content %> -
-

Tags

- <%= render :partial => 'tags/form', - :locals => {:form => post_form} %> -
- <%= post_form.submit %> -
-<% end %> -
- -Note that we have changed the +f+ in +form_for(@post) do |f|+ to +post_form+ to -make it easier to understand what is going on. - -This example shows another option of the render helper, being able to pass in -local variables, in this case, we want the local variable +form+ in the partial -to refer to the +post_form+ object. - -We also add a @post.tags.build at the top of this form. This is to make -sure there is a new tag ready to have its name filled in by the user. If you do -not build the new tag, then the form will not appear as there is no new Tag -object ready to create. - -Now create the folder app/views/tags and make a file in there called -_form.html.erb which contains the form for the tag: - - -<%= form.fields_for :tags do |tag_form| %> -
- <%= tag_form.label :name, 'Tag:' %> - <%= tag_form.text_field :name %> -
- <% unless tag_form.object.nil? || tag_form.object.new_record? %> -
- <%= tag_form.label :_destroy, 'Remove:' %> - <%= tag_form.check_box :_destroy %> -
- <% end %> -<% end %> -
- -Finally, we will edit the app/views/posts/show.html.erb template to -show our tags. - - -

<%= notice %>

- -

- Name: - <%= @post.name %> -

- -

- Title: - <%= @post.title %> -

- -

- Content: - <%= @post.content %> -

- -

- Tags: - <%= @post.tags.map { |t| t.name }.join(", ") %> -

- -

Comments

-<%= render @post.comments %> - -

Add a comment:

-<%= render "comments/form" %> - - -<%= link_to 'Edit Post', edit_post_path(@post) %> | -<%= link_to 'Back to Posts', posts_path %> | -
- -With these changes in place, you'll find that you can edit a post and its tags -directly on the same view. - -However, that method call @post.tags.map { |t| t.name }.join(", ") is -awkward, we could handle this by making a helper method. - -h3. View Helpers - -View Helpers live in app/helpers and provide small snippets of reusable -code for views. In our case, we want a method that strings a bunch of objects -together using their name attribute and joining them with a comma. As this is -for the Post show template, we put it in the PostsHelper. - -Open up app/helpers/posts_helper.rb and add the following: - - -module PostsHelper - def join_tags(post) - post.tags.map { |t| t.name }.join(", ") - end -end - - -Now you can edit the view in app/views/posts/show.html.erb to look like -this: - - -

<%= notice %>

- -

- Name: - <%= @post.name %> -

- -

- Title: - <%= @post.title %> -

- -

- Content: - <%= @post.content %> -

- -

- Tags: - <%= join_tags(@post) %> -

- -

Comments

-<%= render @post.comments %> - -

Add a comment:

-<%= render "comments/form" %> - - -<%= link_to 'Edit Post', edit_post_path(@post) %> | -<%= link_to 'Back to Posts', posts_path %> | -
- h3. What's Next? Now that you've seen your first Rails application, you should feel free to -- cgit v1.2.3