0

In Ruby on Rails, is it possible to display a confirmation message if the user has not entered a string in a text field? I have a form whereby a user enters text in a text field and want to show a confirmation message if they do not enter a certain string. A simple example could be a text field where users can enter items they want to buy from the shop, such as "bread, potatoes, eggs", and so a confirmation message would appear when they click submit saying "You have not entered milk, are you sure you want to proceed?".

I am have the below code working when a user clicks "destroy", and so wonder if this can be developed to work when a user clicks "submit"?

 <%= link_to 'Destroy', email_path(email), method: :delete, data: { confirm: 'Are you sure?' } %>
3
  • The data: {confirm: 'Message'} is assigned to the button when the page is loaded. So you could assign a similar message to a Submit button, but it would always appear. It would not work for a conditional that changes based on user input. The slick way to do it would be on the front end with custom Javascript. To do it with Ruby/Rails, you'll need to test the result on the back end and generate a flash message at the controller level. Commented Jan 24, 2017 at 20:04
  • @moveson I am aware that I could do that and that it would always appear, but is it possible to do one based on what the user inputs? Commented Jan 24, 2017 at 20:06
  • @BenSmith does this answer your question? stackoverflow.com/a/41838032/1536309 if you post your rails form code i can update the example Commented Jan 24, 2017 at 22:09

1 Answer 1

1

You can do this without rails using HTML validation

https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Data_form_validation

<input pattern=".{3,}" required title="Minimum 3 characters required">
<input pattern=".{8, 15}" required title="Input string should be between 8 - 15 characters">

your rails form helpers pass through options to html inputs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.