因为我对Ruby on Rails很新,我会解释我做了什么。我在我的模型中有一个虚拟属性,名为测试。我这样定义它:虚拟属性allways无自定义验证方法
class Comment < ActiveRecord::Base
attr_accessor :testing
attr_accessible :user_name, :comment, :user, :testing
然后我添加了自定义方法,这样的自定义验证:
validate :custom_validation
我还添加过程的方法,:
def custom_validation
# a bit of custom_validation
end
我然后在我的表单中添加一个字段:
<%= form_for(@comment) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<%= f.hidden_field :post_id, :value => @post.id %>
<% if !signed_in? %>
<div class="field">
<%= f.label :user_name %>
<%= f.text_field :user_name, :class => "user_field" %>
</div>
<% else %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<% end %>
<div class="field">
<%= f.label :comment %>
<%= f.text_area :comment, :style => "height: 50px; width: 80%;" %>
</div>
<div class="field pin">
<%= f.label :testint %>
<%= f.text_field :testing, :class => "user_field" %>
</div>
<div class="buttons">
<%= f.submit "Speichern" %>
</div>
<% end %>
这就是我所做的一切。所以,请不要以为我没有别的东西,我没有在这里描述,因为我没有;)
我的问题是,我的虚拟现场测试总是无我custom_validation方法里面。除非我在控制台上运行验证:
co = Comment.new
co.testing = "Hello"
co.valid?
我检查过使用记录器。如果我通过控制台运行测试-场不是零。如果我通过浏览器运行它,它是。看起来该参数以某种方式不能正确传递给模型。我希望我错过了一些非常明显的东西。希望您能够帮助我。
干杯, 迈克尔
你的创建/更新操作是什么样的? – 2012-04-21 22:43:32