Rails 3 – Building a Blog – Part 2: CRUD Show and Create

by woody2shoes on June 30, 2010


The second part of the tutorial for building a blog with Ruby on Rails version 3. We demonstrate how to set up some basic routes, manage the controller and views, and create a basic form for creating posts.
Download 161.4 MB
Download (iphone & ipod) 65.8 MB

  • http://twitter.com/PotHix PotHix

    Æ!!

    Really Cool!
    I really like to develop using cucumber, it shows what I need to do on the next step based on my pre-defined user story.

    I just think that if you are you using a resource route the develop should be easy. :)

    Congrats! I really like this series of screencasts! Keep going!


    PotHix

  • http://twitter.com/PotHix PotHix

    Æ!!

    Really Cool!
    I really like to develop using cucumber, it shows what I need to do on the next step based on my pre-defined user story.

    I just think that if you are you using a resource route the develop should be easy. :)

    Congrats! I really like this series of screencasts! Keep going!


    PotHix

  • http://twitter.com/jeremyhamel jeremyhamel

    I waiting patiently for part 3, do you know when it will be available?

  • http://twitter.com/jeremyhamel jeremyhamel

    I waiting patiently for part 3, do you know when it will be available?

  • Anders

    I’m also eagerly waiting for part 3. It would bee nice to see how you change the look and feel, I’m quite new to rails so it would be bee nice to see how you approach it.

    Great cast!

  • Anders

    I'm also eagerly waiting for part 3. It would bee nice to see how you change the look and feel, I'm quite new to rails so it would be bee nice to see how you approach it.

    Great cast!

  • Frappez_2000

    Here are my show notes:

    # 7/27/2010
    # Notes for TMTC: Rails3-Building a Blog-Part2
    # The following contains some modifications from the screencast
    # Haml is used in these notes instead of Erb
    # Underscored at the beginning of a line represent indentation

    # start episode

    #posts.feature
    Feature: Post Management & Display
    In order to provide quality content
    As an author
    I need to be able to manage posts

    Scenario: 1 post in blog
    Given I have a post
    And my post has title “My Post”
    And my post has body “This is my post. Back off!!!”
    And my post has author with name “Charles Max Wood”
    And my post was published on “May 22, 2010″
    When I display the post
    Then I should see “My Post”
    And I should see “This is my post. Back off!!!”
    And I should see “Charles Max Wood”
    And I should see “May 22, 2010″

    # posts_steps.rb
    When /^I display the post$/ do
    visit “/post/#{@post.id}”
    end

    # routes.rb
    get “post/:id” => “posts#show”

    # posts/show.html.haml
    %h2= @post.title

    # posts_controller.rb
    def show
    @post = Post.find(params[:id]) # Post.find_by_id(params[:id]) used in screencast
    end

    # posts/show.html.haml
    .post_meta
    __%span.post_author
    ____written by
    ____= @post.author.name
    __%span.post_date
    ____on
    ____= @post.published_on.strftime(“%b %d, %Y”)
    .post_body
    __= @post.body

    # posts.feature
    Scenario: Create Post
    Given I have a user named “Charles Max Wood”
    When I go to the posts creation page
    And I fill in “post[title]” with “My Post Title”
    And I fill in “post[body]” with “This is my post. Back off!!! Something here”
    And I select “Charles Max Wood” from “post[author_id]”
    And I fill in “post[published_on]” with “May 23, 2010″
    And I press “Save Post”
    Then I should see “My Post Title”
    And I should see “This is my post. Back off!!! Something here”
    And I should see “Charles Max Wood”
    And I should see “May 23, 2010″

    # posts_steps.rb
    Given /^I have a user named “([^"]*)”$/ do |name|
    User.create(:name => name)
    end

    # features/support/paths.rb
    def path_to(page_name)
    case page_name

    when /the homes?page/
    ‘/’

    when /the post creation page/
    ‘/posts/create’

    # routes.rb
    get “posts/create” => “posts#create”

    # posts_controller.rb
    def create
    @post = Post.new
    end

    # views/posts/create.html.haml
    = form_for @post :url => “/posts/create” do |form|
    __%div
    ____= form.label :title
    ____= form.text_field :title
    __%div
    ____= form.label :body
    ____= form.text_area :body
    __%div
    ____= form.label :author_id
    ____= form.select :author_id, User.all.map {|u| [u.name, u.id]}
    __%div
    ____= form.label :published_on
    ____= form.text_field :published_on
    __%div= submit_tag “Save Post”

    # routes.rb
    post “posts/create” => “posts#create”

    # posts_controller.rb
    def create
    __if request.post?
    ____@post = Post.new(params[:post])
    ____if @post.save
    ______redirect_to “/post/#{@post.id}”
    ____end
    __else
    ____@post = Post.new
    __end
    end

    # All tests pass

    # Terminal
    git add -A . # -A modifier will also remove deleted files
    git commit -m “…”
    git push

    # end episode

  • Frappez_2000

    Here are my show notes:

    # 7/27/2010
    # Notes for TMTC: Rails3-Building a Blog-Part2
    # The following contains some modifications from the screencast
    # Haml is used in these notes instead of Erb
    # Underscored at the beginning of a line represent indentation

    # start episode

    #posts.feature
    Feature: Post Management & Display
    In order to provide quality content
    As an author
    I need to be able to manage posts

    Scenario: 1 post in blog
    Given I have a post
    And my post has title “My Post”
    And my post has body “This is my post. Back off!!!”
    And my post has author with name “Charles Max Wood”
    And my post was published on “May 22, 2010″
    When I display the post
    Then I should see “My Post”
    And I should see “This is my post. Back off!!!”
    And I should see “Charles Max Wood”
    And I should see “May 22, 2010″

    # posts_steps.rb
    When /^I display the post$/ do
    visit “/post/#{@post.id}”
    end

    # routes.rb
    get “post/:id” => “posts#show”

    # posts/show.html.haml
    %h2= @post.title

    # posts_controller.rb
    def show
    @post = Post.find(params[:id]) # Post.find_by_id(params[:id]) used in screencast
    end

    # posts/show.html.haml
    .post_meta
    __%span.post_author
    ____written by
    ____= @post.author.name
    __%span.post_date
    ____on
    ____= @post.published_on.strftime(“%b %d, %Y”)
    .post_body
    __= @post.body

    # posts.feature
    Scenario: Create Post
    Given I have a user named “Charles Max Wood”
    When I go to the posts creation page
    And I fill in “post[title]” with “My Post Title”
    And I fill in “post[body]” with “This is my post. Back off!!! Something here”
    And I select “Charles Max Wood” from “post[author_id]“
    And I fill in “post[published_on]” with “May 23, 2010″
    And I press “Save Post”
    Then I should see “My Post Title”
    And I should see “This is my post. Back off!!! Something here”
    And I should see “Charles Max Wood”
    And I should see “May 23, 2010″

    # posts_steps.rb
    Given /^I have a user named “([^"]*)”$/ do |name|
    User.create(:name => name)
    end

    # features/support/paths.rb
    def path_to(page_name)
    case page_name

    when /the homes?page/
    '/'

    when /the post creation page/
    '/posts/create'

    # routes.rb
    get “posts/create” => “posts#create”

    # posts_controller.rb
    def create
    @post = Post.new
    end

    # views/posts/create.html.haml
    = form_for @post :url => “/posts/create” do |form|
    __%div
    ____= form.label :title
    ____= form.text_field :title
    __%div
    ____= form.label :body
    ____= form.text_area :body
    __%div
    ____= form.label :author_id
    ____= form.select :author_id, User.all.map {|u| [u.name, u.id]}
    __%div
    ____= form.label :published_on
    ____= form.text_field :published_on
    __%div= submit_tag “Save Post”

    # routes.rb
    post “posts/create” => “posts#create”

    # posts_controller.rb
    def create
    __if request.post?
    ____@post = Post.new(params[:post])
    ____if @post.save
    ______redirect_to “/post/#{@post.id}”
    ____end
    __else
    ____@post = Post.new
    __end
    end

    # All tests pass

    # Terminal
    git add -A . # -A modifier will also remove deleted files
    git commit -m “…”
    git push

    # end episode

Previous post:

Next post: