Not Active Record Model Validation

I have a problem with ruby on rails validation

total_book_toy.rhtml

<%= text_field ‘book1’, ‘title1’ %>
<%= text_field ‘book2’, ‘title2’ %>

I want to validate these text_field so user can’t insert same title.
However, I was stuck how to do it.
Or maybe you have another way how to do it.

Any suggestion, please?

Thank you so much.

Use validates_uniqueness_of in the model.
http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000816

If you need more control just define your own validate method.

-Jonathan

Hmm…but these text_field are not related with active record and
database
so we can’t use active record validation with its error_messages, can
we?

in other words, I have not them in my database.

Pls, help me.

Either create a table and just don’t save the object, or use a
tableless model. Here’s an example:

class Contact < ActiveRecord::Base
def self.columns() @columns ||= []; end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s,
default, sql_type.to_s, null)
end

column :name, :string
column :email_address, :string
column :message, :string

validates_presence_of :name, :email_address
end

-Jonathan.

THANK YOU

I have been looking for a way to do table-less ActiveRecords for a
month.

Where would I find complete documentation for this technique ?

Cheers

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs