Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Why does my 'clink_link "New Todo Item"' fail? Thank you!

Here is the code from the create_spec.rb portion of my app. (The commented out section evidently came from the next video. I had been using a Linux system to do my coding but the videos would bail out on me and I was having a hard time getting back to the one that I had been working on. It would invariably put me at least one ahead of where I was supposed to be. A lot of problems to say the least.)

require 'spec_helper'

describe "Adding todo items" do
    let!(:todo_list) { TodoList.create(title: "Grocery list", description:"Groceries") }

    def visit_todo_list(list)
        visit"/todo_lists"
        within "#todo_list_#{list.id}" do
            click_link "List Items"
        end
    end

    it "is successful with valid content" do
        visit_todo_list(todo_list)
        clink_link "New Todo Item"
        fill_in "Content", with: "Milk"
        click_button "Save"
        expect(page).to have_content("Added todo list item.")
        within("ul.todo_items") do
            expect(page).to have_content("Milk")
        end
    end

    #it "displays an error with no content" do
        #visit_todo_list(todo_list)
        #click_link "New Todo Item"
        #fill_in "Content", with: ""
        #click_button "Save"
        #within("div.flash") do
            #expect(page).to have_content("There was a problem adding that todo list item.")
        #end
        #expect(page).to have_content("Content can't be blank")
    #end
end

Here is the view code:

<h1><%= @todo_list.title %></h1>

<ul class="todo_items">
    <% @todo_list.todo_items.each do |todo_item| %>
    <li><%= todo_item.content %></li>
    <% end %>
</ul>

<p>
    <%= link_to "New Todo Item", new_todo_list_todo_item_path %>
</p>

Not enough information. You'd have to show all your code (github?), because the problem may be hidden in a place you didn't expect. Also, we don't know your exact error message. And I mean all of it. It usually tells us all we need to know.

Maciej,

Here are the different modules of code. I uncommented the second error because I believe that the two errors are similar. Thank you for your help!

Bob

Errors:

rspec ./spec/features/todo_items/create_spec.rb:13 # Adding todo items is successful with valid content

Randomized with seed 48598

bob@Kicker:~/Source/projects/odot$ clear

bob@Kicker:~/Source/projects/odot$ bin/rspec --format=documentation spec/features/todo_items/create_spec.rb

Adding todo items
  displays an error with no content (FAILED - 1)
  is successful with valid content (FAILED - 2)

Failures:

  1) Adding todo items displays an error with no content
     Failure/Error: click_link "New Todo Item"
     ActionView::Template::Error:
       undefined local variable or method `todo_list' for #<#<Class:0x00000005fa4718>:0x00000005b8a128>
     # ./app/views/todo_items/new.html.erb:1:in `_app_views_todo_items_new_html_erb__2297046421482950420_47989440'
     # ./spec/features/todo_items/create_spec.rb:26:in `block (2 levels) in <top (required)>'

  2) Adding todo items is successful with valid content
     Failure/Error: clink_link "New Todo Item"
     NoMethodError:
       undefined method `clink_link' for #<RSpec::Core::ExampleGroup::Nested_1:0x000000051f6058>
     # ./spec/features/todo_items/create_spec.rb:15:in `block (2 levels) in <top (required)>'

'create_spec.rb'

require 'spec_helper'

describe "Adding todo items" do
    let!(:todo_list) { TodoList.create(title: "Grocery list", description:"Groceries") }

    def visit_todo_list(list)
        visit"/todo_lists"
        within "#todo_list_#{list.id}" do
            click_link "List Items"
        end
    end

    it "is successful with valid content" do
        visit_todo_list(todo_list)
        clink_link "New Todo Item"
        fill_in "Content", with: "Milk"
        click_button "Save"
        expect(page).to have_content("Added todo list item.")
        within("ul.todo_items") do
            expect(page).to have_content("Milk")
        end
    end

    it "displays an error with no content" do
        visit_todo_list(todo_list)
        click_link "New Todo Item"
        fill_in "Content", with: ""
        click_button "Save"
        within("div.flash") do
            expect(page).to have_content("There was a problem adding that todo list item.")
        end
        expect(page).to have_content("Content can't be blank")
    end
end

'index.html.erb'

<h1><%= @todo_list.title %></h1>

<ul class="todo_items">
    <% @todo_list.todo_items.each do |todo_item| %>
    <li><%= todo_item.content %></li>
    <% end %>
</ul>

<p>
    <%= link_to "New Todo Item", new_todo_list_todo_item_path %>
</p>

'todo_items_controller.rb

class TodoItemsController < ApplicationController
  def index
        @todo_list = TodoList.find(params[:todo_list_id])
  end

  def new
        @todo_list = TodoList.find(params[:todo_list_id])
        @todo_item = @todo_list.todo_items.new
    end

    def create
        @todo_list = TodoList.find(params[:todo_list_id])
        @todo_item = @todo_list.todo_items.new(todo_item_params)
        if @todo_item.save
            flash[:success] = "Added todo list item."
            redirect_to todo_list_items_path
        else
            flash[:error] = "There was a problem adding that todo list item."
            render action: :new
        end
    end

    private
    def todo_item_params
        params[:todo_item].permit(:content)
    end
end

'new.html.erb'

<%= form_for [todo_list, @todo_item] do |form| %>
    <%= form.label :content %>
    <%= form.text_field :content %>

    <%= form.submit "Save" %>
<% end %>

'application.html.erb'

<!DOCTYPE html> <html> <head> <title>Odot</title> <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= csrf_meta_tags %> </head> <body>

<% flash.each do|type, message|%>
<div class="alert flash <% type %>">
    <%= message %>
</div>
<% end %>

<%= yield %>

</body> </html>

9 Answers

I downloaded you code, did bundle install, rake db:migrate and rake db:migrate RAILS_ENV=test and I'm only getting these errors:

Failures:

  1) Creating todo lists displays an error when the todo list has no description
     Failure/Error: expect(page).to_not have_content("Grocery list")
       expected not to find text "Grocery list" in "Grocery list Title Description New Todo list"
     # ./spec/features/todo_lists/create_spec.rb:55:in `block (2 levels) in <top (required)>'

  2) Creating todo lists displays an error when the todo list has no description
     Failure/Error: expect(page).to_not have_content("Grocery list")
       expected not to find text "Grocery list" in "Grocery list Title Description New Todo list"
     # ./spec/features/todo_lists/create_spec.rb:67:in `block (2 levels) in <top (required)>'

OK, first thing you have to to is to correct the word "clink" and make it "click" :)

Maciej,

I actually just found that typo and fixed it. Now I have the same error for both items. I appreciate your help very much!

Bob

Adding todo items displays an error with no content (FAILED - 1) is successful with valid content (FAILED - 2)

Failures:

1) Adding todo items displays an error with no content Failure/Error: click_link "New Todo Item" ActionView::Template::Error: undefined local variable or method todo_list' for #<#<Class:0x000000049df2c0>:0x00000004b90538> # ./app/views/todo_items/new.html.erb:1:inapp_views_todo_items_new_html_erb_4165773648426713150_39613640' # ./spec/features/todo_items/create_spec.rb:26:in `block (2 levels) in <top (required)>'

2) Adding todo items is successful with valid content Failure/Error: click_link "New Todo Item" ActionView::Template::Error: undefined local variable or method todo_list' for #<#<Class:0x000000049df2c0>:0x00000004b8e648> # ./app/views/todo_items/new.html.erb:1:inapp_views_todo_items_new_html_erb_4165773648426713150_39613640' # ./spec/features/todo_items/create_spec.rb:15:in `block (2 levels) in <top (required)>'

OK, so something goes wrong when the todo_list is created usin the let! method. Since I can't tets the code locally, try adding a space after description, before Groceries like this:

let!(:todo_list) { TodoList.create(title: "Grocery list", description: "Groceries") }

I'm not sure if this will change anything (I use factory girl in this test), but you could try. If that doesn't help, there might be some validation we're not passing or something wrong in the todo list create method.

Adding the space after description did not make any changes to the errors so there must be a problem with the todo list create method. Do you have any idea where I might start with fixing that one?

Thank you, Maciej.

Bob

Please show me your todo_list model and controller code. If that doesn't reveal anything, the last thing I can do is take your whole app and run it locally, which would require you to publish it on github.

Maciej,

I am not familiar with factory girl and I don't know how it works but here is the code for the todo_list model and the controller will follow. I went over the code in the controller quickly and noticed that line 67 has the find reference id in brackets. Here id that code: @todo_list = TodoList.find(params[:id]). I don't know if that will make a difference on the params or not. It is toward the end of the module and under private.

Thanks again.

Bob

'todo_list.rb'

class TodoList < ActiveRecord::Base
    has_many:todo_items

    validates :title, presence: true
    validates :title, length: { minimum:3 }
    validates :description, presence: true
    validates :description, length: { minimum:5 }
end

'todo_lists_controller.rb'

class TodoListsController < ApplicationController
  before_action :set_todo_list, only: [:show, :edit, :update, :destroy]

  # GET /todo_lists
  # GET /todo_lists.json
  def index
    @todo_lists = TodoList.all
  end

  # GET /todo_lists/1
  # GET /todo_lists/1.json
  def show
  end

  # GET /todo_lists/new
  def new
    @todo_list = TodoList.new
  end

  # GET /todo_lists/1/edit
  def edit
  end

  # POST /todo_lists
  # POST /todo_lists.json
  def create
    @todo_list = TodoList.new(todo_list_params)

    respond_to do |format|
      if @todo_list.save
        format.html { redirect_to @todo_list, notice: 'Todo list was successfully created.' }
        format.json { render :show, status: :created, location: @todo_list }
      else
        format.html { render :new }
        format.json { render json: @todo_list.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /todo_lists/1
  # PATCH/PUT /todo_lists/1.json
  def update
    respond_to do |format|
      if @todo_list.update(todo_list_params)
        format.html { redirect_to @todo_list, notice: 'Todo list was successfully updated.' }
        format.json { render :show, status: :ok, location: @todo_list }
      else
        format.html { render :edit }
        format.json { render json: @todo_list.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /todo_lists/1
  # DELETE /todo_lists/1.json
  def destroy
    @todo_list.destroy
    respond_to do |format|
      format.html { redirect_to todo_lists_url, notice: 'Todo list was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_todo_list
      @todo_list = TodoList.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def todo_list_params
      params.require(:todo_list).permit(:title, :description)
    end
end

I can't really see anything wrong with the code. If you want to share the cod eon github and link it here, I will take a look at it as a whole and try to fix it locally and then tell you what exactly went wrong (if I succeed that is).

Maciej,

Again, thank you for your time and trouble. I just did sign up for a github account. Is there any easy way to publish the code to my repository? I must have missed it if there is.

Bob

  1. You have to have Git installed and configured on your system. Installation depends on your system, so you have to google it. This should help: http://gitimmersion.com/ You should also follow this whole tutorial at some point in your career, since git is VERY important to know. For now you need to follow the Setup part of Git Immersion.

  2. Do git init in your project folder. Then git add . (including the dot!) and git commit -m "Initial commit"

  3. Go to your GitHub account and create a new repository. When you create it, it will tell you the exact steps to take in order to upload your project to the proper place. In case of problems read their documentation and google error messages.

Maciej,

Thanks again for your help and patience with the challenges I am having with this project. I am more than a little rusty on designing/programming and way out of my comfort zone.

https://github.com/gh4Bob652/Todo_List

Take care.

Bob

Leaving your comfort zone is very good :). I'll chck the project and get back to you.

Maciej,

I appreciate all of the work you have been putting into helping me with this challenge. I think I may know why you got the error you got with my code. I made a slight change on one page and got a couple of different errors, something about something being null. I don't have my other laptop up at this point so that I can check it out again. I believe that it has to do with the fact that I changed the title of the page in question.

It is funny that I got a different error showing up on my Linux machine than what you got. It might be because my Linux laptop is quite old by today's standards. Anyway, I won't be able to do much with that laptop for the next week because I forgot the power cord that goes from the wall to the transformer and I don't have another one on hand here. I just drove 170 miles for the week. Thanks again! Merry Christmas!

Bob

OK, I'll fiddle with the code sometime and let you know what I find :). I use Ubuntu 14.04 64-bit.

In the code on GitHub you have the words "Grocery list" on top of the page permanently, that's why one of the tests (which is, by the way, duplicated) doesn't pass. After I changed it to "Todo lists", everything works fine.

Maciej,

Thank you very much. I have been trying to figure this one out. It would have taken me a long time to figure out what happened. I appreciate all of your help!

Bob

Maciej,

I went back through my files and compared them to what I found on GitHub. I found that I had transposed the titles on the two create_spec files, I had a couple of typos here and there as well. As far as I can tell, my app is now error free, at least for now.

I really appreciate all of your time and expertise. You have been an invaluable help to me. Thank you again. I would imagine that we may be discussing some more of my errors.

Hopefully, since I have figured out how to watch the videos and code at the same time I won't have so much confusion with this course.

Thanks again!

Bob