Forum: Ruby on Rails validation highlighting on non-scaffold fields

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Af64166bb7f9fd025d26582e944a9d1c?d=identicon&s=25 Shane Bouslough (shaneb11716)
on 2005-12-29 15:17
Hi all,

I'm new to rails.

I have created a bunch of CRUD code using scaffolds.
I had to add code to the default output to account for
fields that establish the relationships between tables.

Everything works well, including the validation of the
fields I added.  However, I can't get the highlighting
to work for my fields when they fail validation and the
view is redisplayed.

Here is an example snippet from _form.rhtml:

<!--[form:account]-->
<p><label for="account_number">Number</label><br/>
<%= text_field 'account', 'number'  %>
<br>
<label for="broker_id">Broker</label><br/>
<select id="broker_id" name="broker_id">
	<option value="0">Choose</option>
    <% for broker in @brokers %>
	<option value="<%=broker.id%>"><%=broker.name%></option>
    <% end %>
</select>
</p>
<!--[eoform:account]-->

I think if I used the select helper it would work, but I
can't think of a way to inject my "fake" row of "0, Choose"
as a Broker that forces the user to pick a value from the
dropdown (he legit values are all in the @brokers list).

Any ideas?

-Shane
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2005-12-29 15:29
Shane Bouslough wrote:
> I think if I used the select helper it would work, but I
> can't think of a way to inject my "fake" row of "0, Choose"
> as a Broker that forces the user to pick a value from the
> dropdown (he legit values are all in the @brokers list).
>
> Any ideas?

From the rails docs for the select helper..

:prompt - set to true or a prompt string. When the select element
doesnâ??t have a value yet, this prepends an option with a generic prompt
â?? "Please select" â?? or the given prompt string.

_Kevin
Af64166bb7f9fd025d26582e944a9d1c?d=identicon&s=25 Shane Bouslough (shaneb11716)
on 2005-12-29 16:42
Kevin Olbrich wrote:

> :prompt - set to true or a prompt string. When the select element
> doesnâ??t have a value yet, this prepends an option with a generic prompt
> â?? "Please select" â?? or the given prompt string.

That cleans up my code a lot, thanks.   I also had an invalid
call to errors.ad() which was keeping my field from being
highlighted.

-Shane
This topic is locked and can not be replied to.