Fist, I use rails 3.0.9 and ruby 1.8.7. I need help because I could not check and show it to the user on the screen.
Model:
class Book < ActiveRecord::Base belongs_to :genre validates_presence_of :title, :genre attr_accessible :genre_id end
The form:
<div class="field"> <%= f.label :genre %><br /> <%= f.collection_select(:genre_id, Genre.all(:order => :name), :id, :name, :prompt => 'Select') %> </div>
When I submit the form to empty (with the default value selected by default), the error message appears completely "Genre cannot be empty", but in the form I get html like:
<div class="field"> <div class="field_with_errors"> <label for="book_genre">Genre</label> </div> <br> <select name="book[genre_id]" id="book_genre_id"> <option value="">Select</option> <option value="2">Gramatic</option> <option value="1">Terror</option> </select> </div>
I need a select field inside div.field_with_erros too, because there is a red background that I defined in CSS.
After that, I tried changing f.collection_select: genre_id to: genre
Now I got the select field inside div.field_with_erros. Owwww yeahhh victory, for a few moments, until I understand that validation always blames the error, the rails do not know what the field of choice is: genre: genre_id, which he is looking for.
My question is: how to: change the genre: genre_id in validation? Or do you all have a better way to do this?
Keep testing, I tried changing the label and collection_select to genre_id, and the check works fine, but without generating html with div.field_with_errors, so I tried putting genre_id in validates_presence_of, so now it looks like this: validates_presence_of: title ,: genre ,: genre_id "
Great, but ...
When I submit the form and select the default prompt value, validation, but they blame 2 errors, one: genre and the other: genre_id (¬¬), but the html is ok, the shortcut and select are inside div.field_with_erros.
If I submit the form and choose some value from the genres, validation and html are fine.
If I submit the form, but I change the value of some parameter to one invalid for checking whether validation between models validates validation works fine, but html does not create div.field_with_erros.
Can anyone help me please? (Yes, my English is not the best. Sorry!)