How to Set Default Values for View Helper Method Arguments in Rails 5
Fixing bugs is a key component to development. And in this guide we're going to walk through how to fix a bug in our view pages by setting a default argument value for the login_helper method.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Nice job in going through that last subsection!

We now have a number of our pages styled. They're not finalized like I mentioned before we're going to do things like put a video background here and add some accents and that may also change how we style some of the content. I'm not a big fan of trying to overload the styles in the design on day 1 because I feel like the more the project evolves that should also lead the design and the user experience. But so far I'm liking what I'm seeing and when this guide this is going to be a shorter one.

We are going to focus on how to fix a little bug now. If you click on blog you're going to see that we are going to run into an error. What exactly is this error we're hitting? It says, "Wrong number of arguments for our log in helper". So our login helper by default before had no arguments. But remember in our last guide we added the ability to add styles just like this and that is causing a problem because in other spots where we're calling log in helper throughout the app such as in other layouts we are not passing in an argument. So there are a few ways to get around this.

We could just go in and add log in arguments each time that we call log in helper. That's not really a best practice because then we're going to have to do that forever and there may be times where we don't want a style. And it would be pointless for us to have to pass in just an empty style. That's where Ruby comes in and where we can pass in default values. So if we open up our login helper so I'm going to go to application_helper.rb and you can see at the end of line 2 that we have our style, and if I just go and say equals the empty string what this is essentially going to do is it is going to say that by default style is going to be set.

def login_helper style = ''

So these classes are going to be set but they're just going to be empty strings. So now if I come back here hit refresh. The bug is now gone. But if I come to any other page such as the home page with that layout then all of our styles are still working. As you can see just like this.

1

So it's a way of being able to fix a little bug like that when you have a method that originally didn't have any arguments. And then as time goes on you need to pass arguments to it for some cases but not for all. You can simply set default arguments and that helps fix it.

Let's get into the terminal and say clear and git status we only changed that one file. I'm going to say git ad, then git commit and we'll say Fix bug with log in helper method by setting defaults. And now I can say git push origin design and this is going to be pushed up.

git status
git add 
git commit -m "Fix bug with login helper method by setting defaults"
git push origin design

Now that we have our blog actually fixed and functional. I don't like working on new code or new features. While there are preexisting bugs in the application. I consider that to be a bad practice because if you leave a bug in you may not either remember to go back and fix it or it may creep into other parts of the app without you even realizing it. So whenever I see a bug especially one that won't even let the page render then I like to isolate it fix that and then move on. And that's what we're going to do. So in the next guide we are going to start implementing our blog layout styles.

Resources