在发送之前,我想验证是否存在表单中有两个字段。但问题是控制器的模型在数据库中没有这些字段,所以我尝试制作虚拟属性。我不太确定如何让它工作。
我尝试在模型中执行此操作,称为“Find_number”
class FindNumber < ActiveRecord::Base
attr_accessor :name
attr_accessor :original_number
validates :name, presence: true
validates :original_number, presence: true
end
以及 Find_numbers 控制器的创建操作中的以下内容
def create
@user = current_user
client = Twilio::REST::Client.new(@user.twilio_account_sid, @user.twilio_auth_token)
search_params = {}
%w[in_postal_code near_number contains].each do |p|
search_params[p] = params[p] unless params[p].nil? || params[p].empty?
end
local_numbers = client.account.available_phone_numbers.get('US').local
@numbers = local_numbers.list(search_params)
if :name.valid? && :original_number.errors.any?
unless @numbers.empty?
render 'find_numbers/show'
else
flash.now[:error] = "Sorry, We Couldn't Find Any Numbers That Matched Your Search! Maybe Something Simpler?"
render 'find_numbers/new'
end
else
flash.now[:error] = "Sorry, We Couldn't Find Any Numbers That Matched Your Search! Maybe Something Simpler?"
render 'find_numbers/new'
end
end
当我输入信息时,我得到了错误
undefined method `valid?' for :name:Symbol
我可能错误地调用了 :name 和 :original_number 属性,并且双 if then 语句看起来很新奇 :P。
如果:name.valid,我需要替换什么?&& :original_number.errors.any? ,以确保它验证?或者还有很多我想念的?