Rails 3: connection validation and great user validation

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!)

+4
source share
2 answers

I have created several models to test this. I configure 2 migrations:

 class CreateGenres < ActiveRecord::Migration def change create_table :genres do |t| t.string :title t.timestamps end end end class CreateBooks < ActiveRecord::Migration def change create_table :books do |t| t.string :name t.references :genre t.timestamps end end end 

in the shape of:

 <%= f.label :genre_id %> <%= f.collection_select(:genre_id, Genre.all(:order => :title), :id, :title, :prompt => 'Select') %> 

And in the models I have:

 class Genre < ActiveRecord::Base attr_accessible :title has_many :books end class Book < ActiveRecord::Base belongs_to :genre validates_presence_of :name, :genre_id attr_accessible :name, :genre_id end 

Then the check is done as I expect ...

+1
source

I did not find a way to solve this problem and decided to just check for the error:

 <% if f.object.errors.include?(:genre) %> <div class='field_with_errors'> <% end %> // :genre field <% if f.object.errors.include?(:genre) %> </div> <% end %> 

Some links:

0
source

Source: https://habr.com/ru/post/1447526/


All Articles