Hartl Chapter 5, Section 5.6, Exercise 4

I am still getting an error when running $bundle exec rake test:

ArgumentError: wrong number of arguments (0 for 1)
     app/helpers/application_helper.rb:2:in `full_title'

“On line 2, you need to pass exactly one argument. See http://ruby-doc.org/core-2.2.0/ArgumentError.html

Looking here: http://ruby-doc.com/docs/ProgrammingRuby/html/tut_methods.html I saw that a method with one argument looks like:

def myNewMethod(arg1, arg2, arg3)     # 3 arguments
  # Code for the method would go here
end

def myOtherNewMethod                  # No arguments
  # Code for the method would go here
end

def myOtherNewMethod(arg1)
end

So why am I still getting an error? My ‘app/helper/application_helper.rb file looks like this:

module ApplicationHelper
    def full_title(page_title)
     base_title = "Ruby on Rails Tutorial Sample App"
      if page_title.empty?
        base_title
          else
        " #{page_title} | #{base_title}"
      end
    end
end

So isn’t ‘def full_title(page_title)’ passing only one argument?


[July 11, 2015 – EDIT] I was able to figure out my error. I had to pass an argument through the full_title method:

def full_title(page_title = " ")
  base_title = "Ruby on Rails Tutorial Sample App"
  if page_title.empty?
    base_title
  else
    "#{base_title} | #{page_title}"
  end
 end

I had thought I did because of ‘(page_title)’ but (page_title) is different from (page_title = ” “). Since the title is a string that I am passing, I needed to set page_title equal to an empty string.

Advertisements

5.3.2 of Hartl Ruby on Rails

Today was so frustrating. I am halfway through Chapter 5 and followed the instructions word for word. It’s been mostly a review (for me anyway) of CSS and HTML.

When I ran $bundle exec rake test in section 5.3.2 Rails Routes, I kept getting 4 errors. The error message was:Unnamed QQ Screenshot20150709113038

So the error messages were pretty clear, right? My web pages are saying “Ruby on Rails Tutorial Sample App | About” instead of “About | Ruby on Rails Tutorial Sample App”. I just couldn’t figure out how to fix it and where the error was coming from.

Finally, while sitting in an iOS workshop this evening, it hit me. The only piece of code that I copied and pasted into my application was from my previous blog post that I had found on Stack Overflow. Coming back home, I opened up the page and lo and behold, there was my error!

The error was located in the app/helpers/application_helper.rb file.

I had to change this:

module ApplicationHelper
    def full_title(page_title)
      base_title = "Ruby on Rails Tutorial Sample App"
        if page_title.empty?
          base_title
        else
          "#{base_title}| #{page_title}"
        end
    end
end

To this:

module ApplicationHelper
    def full_title(page_title)
  base_title = "Ruby on Rails Tutorial Sample App"
      if page_title.empty?
    base_title
      else
    " #{page_title} | #{base_title}"
      end
    end
end

SO SIMPLE!