English 中文(简体)
铁路服务器-边验证-属于模型
原标题:Rails Server-side validations - belongs_to model

I m stuck. Trying to get to what would seem like a simple validation working, but I still can t get this to work.

在座各位:

模型:

class Course < ActiveRecord::Base

 has_many :locations, :dependent => :destroy
 accepts_nested_attributes_for :locations, :allow_destroy => true

end

class Location < ActiveRecord::Base

  belongs_to  :course

  validates_presence_of :city,
                    :message => "City cannot be blank",
                    :allow_blank => true,
                    :if => Proc.new {|location| location.nil? || (!location.nil? && location.course.format !=  Live ) } 

end

我想要做的是,如果某个课程有任何东西,那么我会做些什么。 选择了住所,然后可以节省一个空白城市。 如果是的话,可以选择。 格式是真的,留下了一个错误。

我认为,在没有确定地点的情况下,它正在着手进行验证工作。 但是,在更新课程和改变生活模式时,验证工作并不奏效。

在铁路方面,你如何像我前面的表格一样,总是参考另一个栏目? 如果是新课程,那么该课程的开端是,在检查验证时,就没有设置了。 确实如此。

提前感谢!

问题回答
validates_presence_of :city,
                    :message => "City cannot be blank",
                    :allow_blank => true,
                    :if => Proc.new {|location| location.nil? || (!location.nil? && location.course.format !=  Live ) } 

In above validation :allow_blank has no relation with :if block. Try like this

validates_presence_of :city,
                    :message => "City cannot be blank",
                    :if => Proc.new {|location|  your_conditions } 

Now this validation works when  your_condition  will return true.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签