Recently I have been working on a project using Ruby on Rails. I came across a requirement to Dynamically change the button text on a form partial.
When using a form partial, you are able to use the same block of HTML / ERB code for both create and edit. However the button text for both controller methods would be the same.
The way I have got round this is to do the following.
<%= f.submit @product.persisted? ? "Update" : "Create" %>
In my example, the form that I am working with either creates or updates a Product. By using a shorthand if statement I am able to change the text on the button by checking if the object is already persisted.
When editing an object it will already be persisted to the database, when creating a new object it will not yet be persisted.
This is just one way of achieving the desired functionality, have you come across any other ways? Please leave a comment I would be interested to hear your feedback.