我想在我的某個視圖中檢索ActiveRecord字段的最大長度驗證。正在檢索ActiveRecord長度驗證返回零
在軌控制檯以下罰款作品,並返回正確的值:
irb(main):046:0> micropost = Micropost.new
=> #<Micropost id: nil, content: nil, user_id: nil, created_at: nil, updated_at: nil>
irb(main):047:0> micropost._validators[:content][1].options[:maximum].to_s
=> "140"
然而,當我在我的控制器使用相同的代碼則返回nil:
class PagesController < ApplicationController
def home
@title = "Home"
if signed_in?
@micropost = Micropost.new
@feed_items = current_user.feed.paginate(:page => params[:page])
@content_max = @micropost._validators[:content][1].options[:maximum].to_s
end
end
...
end
我也試着包括方法,在我的ApplicationHelper,這也返回零;-(
def content_max
Micropost._validators[:content][1].options[:maximum].to_s
end
w ^我做錯了什麼?
我的解決方案適合您嗎? –