Basic TDD in Rails : Update an Article Feature

Objective


I want to edit an existing article and update the title and description.

Steps


Step 1

Add the second feature spec:

  it 'can edit an existing article' do
    Article.create(title: 'test', description: 'test')
    visit articles_path

    click_link 'Edit'
    fill_in "Title", with: 'Test Article'
    fill_in "Description", with: 'Body of the article'
    click_button 'Submit'

    expect(page).to have_text('Test Article')      
    expect(page).to have_text('Body of the article')      
  end

Step 2

Run the test.

  1) Manage Articles can edit an existing article
     Failure/Error: click_link 'Edit'
     Capybara::ElementNotFound:
       Unable to find link "Edit"

Step 3

Add the edit link to the articles index page.

<%= link_to 'Edit', edit_article_path(article) %>

Step 4

Run the test:

$ spec spec/features/manage_articles_spec.rb 

You get the error:

 1) Manage Articles can edit an existing article
     Failure/Error: Unable to find matching line from backtrace
     AbstractController::ActionNotFound:
       The action 'edit' could not be found for ArticlesController

Step 5

Add the edit action to articles controller:

  def edit

  end

Step 6

Run the test.

1) Manage Articles can edit an existing article
     Failure/Error: Unable to find matching line from backtrace
     ActionView::MissingTemplate:
       Missing template articles/edit, application/edit with {:locale=>[:en], :formats=>[:html, :xml], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
         * "/Users/zepho/projects/tdd/basics/blog/app/views"

Step 7

Create views/articles/edit.html.erb and add the view for edit.html.erb:

<%= form_for @article do |f| %>
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </p>

  <p>
    <%= f.submit 'Submit' %>
  </p>
<% end %>

Step 8

Run the test.

 1) Manage Articles can edit an existing article
     Failure/Error: Unable to find matching line from backtrace
     ActionView::Template::Error:
       First argument in form cannot contain nil or be empty

Step 9

Change the edit action as follows:

  def edit
    @article = Article.find(params[:id])
  end

Step 10

Run the test.

  1) Manage Articles can edit an existing article
     Failure/Error: Unable to find matching line from backtrace
     AbstractController::ActionNotFound:
       The action 'update' could not be found for ArticlesController

Step 11

Add the update action to the articles controller:

  def update
    allowed_params = params.require(:article).permit(:title, :description)
    article = Article.find(params[:id])
    article.update_attributes(allowed_params)

    redirect_to articles_path
  end

Step 12

Run the tests. All the tests pass.

Step 13

Let's use partials, change the articles/edit.html.erb:

<h1>Edit Article</h1>

<%= render 'form' %>

Change the articles/new.html.erb:

<h1>New Article</h1>

<%= render 'form' %>

Step 14

Create a form partial app/views/articles/_form.html.erb:

<% if @article.errors.any? %>
  <h2><%= pluralize(@article.errors.count, "error") %> prohibited
    this article from being saved:</h2>

  <ul>
  <% @article.errors.full_messages.each do |m| %>
    <li><%= m %></li>
  <% end %>
  </ul>
<% end %>


<%= form_for @article do |f| %>
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </p>

  <p>
    <%= f.submit 'Submit' %>
  </p>
<% end %>

Step 15

Run :

rake spec

All tests should pass.

Summary


In this lesson we wrote feature spec for updating an article feature. We also used partials to remove view duplication.


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.