سؤال

Using Ruby formhelper,

I want the user to see a popup like "fill this out" when they try to submit the form with any empty fields or unselected selects.

To require a text field, this works:

<%= f.text_field :name, :required => "required" %>

To require a select, I'm trying this but it doesn't work:

<%= f.collection_select :metric, Metric.all, :id, :name, :prompt => true, :required => "required" %> 

The select is there and its options are populated correctly in the dropdown. But the user should see a popup if they try to submit without selecting another option besides the default "please select" - it never appears.

هل كانت مفيدة؟

المحلول

You need to use Ruby's select tag combined with Ruby's options_from_collection_for_select. You can set your prompt to true or add a custom prompt and required must be in curly braces and its value set to true. For example:

<%= select('trophy','metric', options_from_collection_for_select(Metric.find(:all), :id, :name),{:prompt => 'Select Metric'},{:required => true})%>

Where:

-trophy is the name of an instance variable or a model object

-metric is is the attribute of that instance variable. This is typically a field/column of the table whose data you're displaying.

-:id is the key

-:name is the result

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top