我在模型中有一点检查:
class Customers < ActiveRecord::Base
def check_user_name(name, email)
name = Customers.where(:name => name)
email = Customers.where(:email => email)
if name && email
@answer = 'Question was send to us. Thank you.'
else
@answer = 'ERROR, no such name or email.'
end
end
end
和查看(haml文件):
=@answer
但在页面上没有文字......空......请解释我为什么?)
答案 0 :(得分:1)
试试这个:
class Customers < ActiveRecord::Base #why this model plural?
def check_user_name(name, email)
result = {}
result[:name] = where(:name => name)
result[:email] = where(:email => email)
end
end
class CustomersController < ApplicationController
result = Customers.check_user_name(name, email)
if result[:name].present? && result[:email].present?
@answer = 'Question was send to us. Thank you.'
else
@answer = 'ERROR, no such name or email.'
end
end
=@answer
控制器中的所有实例变量都传递给视图
答案 1 :(得分:0)
您的模型中的实例变量无法从您的视图中访问。
controller 中的实例变量是您将数据传递到视图的方式。
尝试从模型方法返回字符串,然后将其分配给控制器中的变量@answer
。例如。 @answer = cust.check_user_name(params[:name], params[:email])