John Oggy
John Oggy

Reputation: 319

How get variable value using HAML?

I have a little check in model:

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

and view (haml file):

=@answer

But on page is no text...empty....please, explain me WHY ?)

Upvotes: 1

Views: 2330

Answers (2)

CuriousMind
CuriousMind

Reputation: 34145

Try this:

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

all instance variables in controller are passed into views

Upvotes: 1

sevenseacat
sevenseacat

Reputation: 25029

Instance variables in your model are not accessible from your view.

Instance variables in your controller are how you pass data to your view.

Try just returning the string from your model method, and then assigning it to the variable @answer in your controller. eg. @answer = cust.check_user_name(params[:name], params[:email])

Upvotes: 0

Related Questions