1

I am new to rails and regular expressions. I am trying to make an application in which Users can register with one of two types of email address: user@a.edu or user@b.edu. I am making a page that shows all users that are not the type of the current user. For example, jason@a.edu were logged in, the page would display all users of type b. If lauren@b.edu were logged in, the page would display all users of type a. I am trying to use regular expressions to know what type of user is logged in based on the email address and dynamically generate the page when the user clicks the link. I have created this method in the model:

def other_schools
   if /.+@a\.edu/.match(current_user.email)
      User.where(email != /.+@a\.edu/)
   else
      render :text => 'NOT WORKING', :status => :unauthorized
   end
end

Here is the controller:

def index
    #authorize! :index, :static_pages 
    @users = current_user.other_schools
end

Here is the view that displays each user:

<% @users.each do |user| %>
          <li class="span3">
                <div class="thumbnail" style="background: white;">
                  <%= image_tag "idea.jpeg" %>
                  <h3><%= user.role %></h3>
                  <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
                  <a class="btn btn-primary">View</a>
                </div>
          </li>
<% end %>

The view simply loops through the @user object. When I try to load the page, I am told that there is an undefined local variable or method `current_user'. How do I fix this?

Philip7899
  • 4,599
  • 4
  • 55
  • 114

1 Answers1

1

Your models are not "aware" of the helpers method. Current_user is one of them. so you need to pass the user object to the function / use the current user instance to fetch results:

# controller
def index
    #authorize! :index, :static_pages
    @users = User.other_schools(current_user)
end

# User model
def self.other_schools(user) # class method
   if user.email.match(/.+@a\.edu/)
      User.where("email NOT LIKE '%@a.edu'")
   else
      User.where('false') # workaround to returns an empty AR::Relation
   end
end

Alternative (using the current_user instance):

# controller
def index
    #authorize! :index, :static_pages
    @users = current_user.other_schools
    if @users.blank?
        render :text => 'NOT WORKING', :status => :unauthorized
    end
end

# User model
def other_schools # instance method
   if self.email.match(/.+@a\.edu/)
      User.where("email NOT LIKE '%@a.edu'")
   else
      User.where('false') # workaround to returns an empty AR::Relation
   end
end
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
  • Thanks. I tried implementing the alternative version but now it is saying there is an undefined method 'each' in the view. That makes no sense to me. I made an edit and put the view into the question content above. – Philip7899 Oct 21 '13 at 17:59
  • I just updated my answer @Philip7899 the problem is that you tried to do a render in the model, but it should be done in the Controller – MrYoshiji Oct 21 '13 at 18:10
  • Thanks. Now it is rendering 'not working', but when it should actually be working. I think it has to do with my regex. Do you know if the regex I have are correct? – Philip7899 Oct 21 '13 at 18:31
  • No it's not correct, for PG SQL you need to use this syntax: `.where("email =~ ?", /.+@a\.edu/)` --- what is your DBMS? MySQL? PostGreSQL? MongoDB? – MrYoshiji Oct 21 '13 at 18:32
  • I am trying to say that the emails are not equal. – Philip7899 Oct 21 '13 at 18:35
  • OH! Try using this instead: `User.where("email NOT ILIKE '*@*.edu'")` – MrYoshiji Oct 21 '13 at 18:37
  • What happened to the 'a'? – Philip7899 Oct 21 '13 at 18:41
  • `User.where("email NOT ILIKE '*@a.edu'")` if you want to keep the a (this wildcard will match every email finishing with exactly "@a.edu") – MrYoshiji Oct 21 '13 at 18:43
  • I am using sql so I had to switch NOT ILIKE to NOT LIKE. Either way, both emails im testing with are all lower case so it shouldn't matter. Currently, its printing users from both schools. So the if statement is evaluating to true but the regex User.where("email NOT ILIKE '*@a.edu'") is still not working. – Philip7899 Oct 21 '13 at 18:52
  • Pff my bad, end of the day, tired... `User.where("email NOT LIKE '%@a.edu'")` instead (using % not *) – MrYoshiji Oct 21 '13 at 18:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39675/discussion-between-philip7899-and-mryoshiji) – Philip7899 Oct 21 '13 at 19:15