ZubatMan ZubatMan - 11 months ago 53
Ruby Question

Separate value from text on Rails form_for submit button

I have a Rails 4 application that is using form_for to build the form for a basic Blog type, everything is working as expected except for one thing: I have to determine the publish state of the blog based on the submit button chosen by the User when they submit the form. Here is the HAML code I have for that:

= form.submit 'Save as Draft', value: 'Draft', name: 'publish_state', class: 'button'
= form.submit 'Publish', value: 'Published', name: 'publish_state', class: 'button'

In this setup there are two different states
, both are treated as Strings in the Database.

The issue that I'm having with this is that even though I am specifying the buttons to say
'Save as Draft'
they are only displaying based on their values,
respectively. I have searched around and could only find how to either specify the value or the text, not separate the two. Is there a way in Rails forms to allow for this? Or, if not Rails forms, what are some other approaches that have worked?


tl;dr - how can I make a button in Rails forms show one String and return a different one when clicked?

Answer Source

You could use a generic button as the submit button.

= form.button 'Save as Draft', value: 'Draft', name: 'publish_state', class: 'button', type: 'submit'
= form.button 'Publish', value: 'Published', name: 'publish_state', class: 'button', type: 'submit'