0

Application controller has a helper methods : current_user is accessible from all my views. But pro_user is completely inaccessible from my view.

    def current_user
      @current_user ||= User.find_by_auth_token( cookies[:auth_token]) if cookies[:auth_token]
      rescue ActiveRecord::RecordNotFound
    end
    helper_method :current_user

   def pro_user

        @pro_user ||= Subscription.where(:email => current_user.email).pluck(:email) 
        return @pro_user
        rescue ActiveRecord::RecordNotFound

   end
   helper_method :pro_user 

Now in the view, if i access pro_user its always nil. But funnily I have a method called current_user also in the application controller which is a helper_method. It's working fine in the view.

Please can you help me?

4
  • I don't understand the use-case of your query, Subscription.where(:email => current_user.email).pluck(:email)? When you already have the current_user.email why to pluck the same email from subscriptions table by firing the query?
    – Kirti Thorat
    Apr 30, 2014 at 21:18
  • Kirti current_user.email gets stored in Subscription table after user successfully purchases. pro_user method is below the current_user method in the application controller to check if user is a premium aka pro_user. In the view I was trying to see if pro_user.email.blank? and its nil all the time. I also tried @pro_user in the view. Everything prints correctly in the application controller but its nil in the view. Apr 30, 2014 at 21:29
  • Kirti, you have helped me 2 times before alread! So I am hoping you can shed some light into this again :) Apr 30, 2014 at 21:36
  • Your method is accessible in the view, the problem is with the query which is returning 0 records. You need to figure out why Subscription records were not saved for current_user. Follow meager's answer. He has given a thorough explanation of your problem.
    – Kirti Thorat
    Apr 30, 2014 at 21:42

1 Answer 1

3

Your'e misdiagnosing your problem. It's working just fine, and the view can definitely see your method, but your method is returning nil. Your method isn't returning anything.

If the method actually weren't accessible, you'd see an exception to that effect. Ruby doesn't simply resolve unknown methods to nil.

In the view I was trying to see if pro_user.email.blank? and its nil all the time.

That isn't a valid way of using the value returned by the method you posted. It returns an array.

I also tried @pro_user in the view. Everything prints correctly in the application controller but its nil in the view.

Neither is that. The method returns an array of zero or more email addresses, this is what pluck does is for. The return value will never respond to .email. You would need to use pro_email.any? to test whether the array contains any items.

You also can't simply access @pro_user. That doesn't invoke the method, that access a member variable which will be nil by default.

I do <% if pro_user.blank? %> { ..do blah.. }

That's not valid erb syntax. Assuming that your syntax is actually correct, blank? will return true for an empty array. The method is returning [], not nil. There is no error, you just need to figure out why your query returns zero records.

5
  • I removed the recuse statement and added return @pro_user at the end of the method. Still no use. Apr 30, 2014 at 21:24
  • @user2511030 How are you invoking the method?
    – user229044
    Apr 30, 2014 at 21:33
  • In the view : I do <% if pro_user.blank? %> { ..do blah.. } Apr 30, 2014 at 21:35
  • meagar do you mean i have to do pro_user.any? What i a having difficulty with is the current_user method which is very similar to pro_user is accessible in all my views. I can do @current_user, current_user.email in all the views and it works beautifully. Apr 30, 2014 at 21:39
  • meagar, yes it will return [] if the user is not a pro user and return ["[email protected]"] if successfully payment was received. So i have been doing .blank? empty? nil? on the pro_user in the view. I will try .any? Apr 30, 2014 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.