Dynamically Control Button Labels With a Helper Method

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.

def sign_up_btn
  if User.sign_up_allowed == true
    "Prevent new account creation"
    "Allow new account creation"

Or, if you want to be fancy, try this refactored-while-in-the-midst-of-blogging-version.

def sign_up_btn
  User.sign_up_allowed ? "Prevent new account creation" : "Allow new account creation"

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.

<%= button_to action: "disallow_sign_up" do %><%= "#{sign_up_btn}" %><% end %>

(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.)

def disallow_sign_up
  User.sign_up_allowed ? User.sign_up_allowed = false : User.sign_up_allowed = true
  redirect_to users_index_path

I have a serious fixation on the ternary operator, if you haven’t noticed.