1

我有一点检入模型:

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

但是页面上没有文字......空......请解释我为什么?)

4

2 回答 2

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

控制器中的所有实例变量都传递到视图中

于 2013-04-19T12:27:41.433 回答
0

您的模型中的实例变量无法从您的视图中访问。

控制器中的实例变量是您将数据传递到视图的方式。

尝试从模型方法中返回字符串,然后将其分配给@answer控制器中的变量。例如。@answer = cust.check_user_name(params[:name], params[:email])

于 2013-04-19T12:18:37.853 回答