Jump to content

The ultimate community for Ruby on Rails developers.


Photo

shared variables, controller, views and helper methods.


  • Please log in to reply
3 replies to this topic

#1 jschrock1

jschrock1

    Dispatcher

  • Members
  • 45 posts
  • LocationIndianapolis

Posted 05 November 2013 - 08:05 PM

I just read a good article on shared varaibles http://www.devarticl...uby-on-Rails/2/   Then I tried something that I thought should work, but does not...  Here is my set up.

 

My controller has:

 

def following
    me = User.find_by(email: "example-5@railstutorial.org")
    current_user = me
    puts "Jay Test if @current_user is there....  " + @current_user.email

 

My sessions_helper contains:

 

Module SessionsHelper

...

  def current_user=(user)
    logger.debug("Jay in current_user= Start")
    @current_user = user  #This sets the instance variable.
  end

...

 

The error I receive after running a scenario where I click on a "following" button that invokes the following def in my controller as noted above is  "no method available for nil object @current_user"  (or something similar to this).  I was certainly expecting @current_user to be a valid materialized object.  Since it error-ed, I'm scratching my head and googling shared variables again...  ugg Any ideas on why this is not working? Should it work and perhaps I have some other error?

 

My understanding is that @current_user will be an instance variable for the sessions_helper module.  Actually perhaps since its a module and not a class will this cause the issue I'm seeing?  Can a module have instance attributes, but not accessible to a controller?

 

thanks again in advance, Jay.



#2 jschrock1

jschrock1

    Dispatcher

  • Members
  • 45 posts
  • LocationIndianapolis

Posted 05 November 2013 - 08:16 PM

I just read some more information on a module vs a class, suspecting my problems with instance attributes may involve using the sessions_helper module.  Im gathering that a module is similar to a Java static class, so you can not instantiate a module which now I realize (duuu) as I never instantiated the session_helper.  But it does allow for instance attributes to be created such as @current_user, but but , but how can it have instance attributes if it can never be an instance of an object??  Im still missing something here...



#3 RichOnRails

RichOnRails

    Signalman

  • Members
  • 10 posts

Posted 05 November 2013 - 11:43 PM

I just read some more information on a module vs a class, suspecting my problems with instance attributes may involve using the sessions_helper module.  Im gathering that a module is similar to a Java static class, so you can not instantiate a module which now I realize (duuu) as I never instantiated the session_helper.  But it does allow for instance attributes to be created such as @current_user, but but , but how can it have instance attributes if it can never be an instance of an object??  Im still missing something here...

 

Actually, if i'm understanding you correctly, you really want to define current_user in your controller, and then use helper_method to share it with your helper.

 

I could be wrong though, because i don't quite understand what you are trying to accomplish.



#4 jschrock1

jschrock1

    Dispatcher

  • Members
  • 45 posts
  • LocationIndianapolis

Posted 06 November 2013 - 02:40 PM

Hi Rich, I was just testing out some things, my test is not anything realistic,   I just wanted to see of I call a method in a helper (sessions_helper) and that helper method created a variable @current_user, do I then have access to it in my controller noting that its all done in one transaction, no page refresh or anything.  My results indicate you can not get access to @current_user.  So that started getting me to jossle my mind a bit (after going through most of the tutorial) thinking what did I forget, or what don't I understand... 

 

Since then I understood via going back to my notes that variables are shared between controller and views, but my notes did not specify the session_helper module.  Then I started wondering about what a ruby "module" is, and found a slue of threads describing it more or less as a java static class.  Then I started wondering if it is like a static class, and if you create variable like @current_user or @foo_bar  what exactly are these variables?  Can't be instance variables as the module is not instantiated as in instance.  My guess is perhaps they are all local variables within their methods or does rails provide some magic in this area.  I love Rails magic, but it can be quite frustrating to find out the ins and outs of it...

 

Its kind of funny, and I know its not the right thing, but im trying to build a web site using ruby and rails, getting there bit by bit,  but at the same time trying to get through this tutorial, and at the same time trying to learn Ruby....  oh my!  I wish I can clone myself or at least become Amish and just take it one step at a time!!  But its tough when you got ants in your pants!!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users