Reader beware: I’m learning Rails right now. In no way, shape or form should this post or any posts here for at least the next month and a half be taken as gospel truth of how to do things right in Rails. They probably are way, way out in right field.
Okay, say you have a view and in that view you have a beautiful button. If you want to assign a name to that button dynamically, say based on the truthiness of some variable, here’s an easy way to do it.
Open up the helper.rb file for your model. In your helper file, write a method that will provide you two pieces of text, depending on a certain condition.
1 2 3 4 5 6 7
Or, if you want to be fancy, try this refactored-while-in-the-midst-of-blogging-version.
1 2 3
Then, back over in your view file, add this bit to create the button and assign it a title based on the results of the method above.
(Just so you get a fuller picture, here’s my disallow_sign_up method from my users_controller. I wrote it as a way for an admin to turn off access to the sign up page in the expense tracking app I’m working on.)
1 2 3 4
I have a serious fixation on the ternary operator, if you haven’t noticed.