Jump to content

The ultimate community for Ruby on Rails developers.


Photo

create an attribute in action, does not stay materiales as an instance attribute


  • Please log in to reply
4 replies to this topic

#1 jschrock1

jschrock1

    Dispatcher

  • Members
  • 48 posts
  • LocationIndianapolis

Posted 17 December 2013 - 09:59 PM

I always thought, and have coded where if you have an attribute coded as @something it will behave as an instant attribute in your controller and accesible to your view.

 

Well I have the following method:

def search_all_open
     @tasks_search = Task.where(:status == "A")
     respond_to do |format|
         format.html { redirect_to current_user, notice: 'User was successfully updated bub.' }
         format.json { head :no_content }
     end
  end

the variable @tasks_search gets created, contains many records, but when the "show" action is invoked via logic in this method, the @task_search in the "show" action is nil and nil in my view that needs it.   Why??? What am I missing here?  two hours down the drain! crap! I took out the puts to verify that it is indeed getting created in this method.  The only difference is that this is an additional action (obviously) aside from what rails gives you (show, index, create etc..)  Would this make a difference?

 

My Show action below fails on the second puts,  "no count method on nil object"

def show
  	puts "++++++++++++++++++++++++++++++++  Start of show controller: " + current_user.to_s
	puts " ++++++++++++++++ in show @tasks_search is: " + @tasks_search.count.to_s
	 @my_job_sub_categories = current_user.job_sub_categories 
	 @my_tasks = Task.where(runner_id: current_user.id)
	 puts "++++++++++++++++++++++++++++++ @my_tasks: " + @my_tasks.to_s
  end


#2 jschrock1

jschrock1

    Dispatcher

  • Members
  • 48 posts
  • LocationIndianapolis

Posted 18 December 2013 - 03:16 AM

 

I always thought, and have coded where if you have an attribute coded as @something it will behave as an instant attribute in your controller and accesible to your view.

 

Well I have the following method:

def search_all_open
     @tasks_search = Task.where(:status == "A")
     respond_to do |format|
         format.html { redirect_to current_user, notice: 'User was successfully updated bub.' }
         format.json { head :no_content }
     end
  end

the variable @tasks_search gets created, contains many records, but when the "show" action is invoked via logic in this method, the @task_search in the "show" action is nil and nil in my view that needs it.   Why??? What am I missing here?  two hours down the drain! crap! I took out the puts to verify that it is indeed getting created in this method.  The only difference is that this is an additional action (obviously) aside from what rails gives you (show, index, create etc..)  Would this make a difference?

 

My Show action below fails on the second puts,  "no count method on nil object"

def show
  	puts "++++++++++++++++++++++++++++++++  Start of show controller: " + current_user.to_s
	puts " ++++++++++++++++ in show @tasks_search is: " + @tasks_search.count.to_s
	 @my_job_sub_categories = current_user.job_sub_categories 
	 @my_tasks = Task.where(runner_id: current_user.id)
	 puts "++++++++++++++++++++++++++++++ @my_tasks: " + @my_tasks.to_s
  end

Could it be that a redirect_to link instantiates a new instance of my controller??? 



#3 Ohm

Ohm

    Driver

  • Moderators
  • 410 posts
  • LocationCopenhagen

Posted 18 December 2013 - 07:34 AM

Instance variables, that is variables with an @ in front, will live for as long as the instance lives.

 

When you redirect a user, you are essentially saying: "I'm done, go find new stuff here: http://www.example.com/". The web server will forget about you (because that is what web servers do (http://xkcd.com/869/)) and you'll make a new request to the new URL with a blank slate.

 

If you want the variable to persist, you'll need to put it in a session variable. However, you probably do not want a ActiveRecord relation (which is evaluated) in your session.


Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk


#4 jschrock1

jschrock1

    Dispatcher

  • Members
  • 48 posts
  • LocationIndianapolis

Posted 18 December 2013 - 01:08 PM

Instance variables, that is variables with an @ in front, will live for as long as the instance lives.

 

When you redirect a user, you are essentially saying: "I'm done, go find new stuff here: http://www.example.com/". The web server will forget about you (because that is what web servers do (http://xkcd.com/869/)) and you'll make a new request to the new URL with a blank slate.

 

If you want the variable to persist, you'll need to put it in a session variable. However, you probably do not want a ActiveRecord relation (which is evaluated) in your session.

 

Yes, I did find another thread indicating the same situation.  I guess that makes some sense.  For some reason I had related a controller to a servlet concept..  ugg.. ok..  Well, I think I've hit my frist real design problem.  Perhaps I can explain it...

 

Up intil now I've just been coding "Plumbing" concepts, or proof of concepts, with no inent to make any fancy web pages... mainly to extend the tutorial knowledge and try to get through the basic issues of rails...  And im still in this mode... 

 

I have a simple user object displaying the users information, name, address etc..  Then I've started to add things to this "profile" page which is always invoked via the "show" action.  The intent is to make this page more or less a dashboard (still in plumbing mode), so in addition to showing the user information I've started adding some relationships..  For example I know have added a list of all the tasks assigned to this user., I've also added a list of all his users skills...  These two relationships I've created in my "show" action and therefor quite easily added to the show view.

 

Now I've wanted to added a number of buttons all related to various search options..  For example "search_for_all_tasks", then "search_for_closed_tasks.. etc.  This is where I've lost a design pattern.  Thinking I simply create a search_action, have this method create relationship lists as instance attributes, redirect to "show" action then my show view I would have added an area to show the search results.

 

I'm beginning to think I need to move away from pulmbing concepts and figure out a better design pattern for such a simple thing like this.  I hate to create global variables, don't want it in session, so im missing something here.  Just thinking out loud, would I perhaps need to store the search list in the user model?  Or do I simply create the relationship in the view?  @tasks_search = Task.where(:status == "A"), but then I still need something to tell me what type of search it is, (what search button did he click)  status = A, or status = "I" or status = "C" etc...  Right now I see the only solution that can fit in my little brain is to store it in the user model....  If you have a better design pattern I'd be indebted to you for ever if you could splain it...



#5 jschrock1

jschrock1

    Dispatcher

  • Members
  • 48 posts
  • LocationIndianapolis

Posted 18 December 2013 - 08:54 PM

Yes, I did find another thread indicating the same situation.  I guess that makes some sense.  For some reason I had related a controller to a servlet concept..  ugg.. ok..  Well, I think I've hit my frist real design problem.  Perhaps I can explain it...

 

Up intil now I've just been coding "Plumbing" concepts, or proof of concepts, with no inent to make any fancy web pages... mainly to extend the tutorial knowledge and try to get through the basic issues of rails...  And im still in this mode... 

 

I have a simple user object displaying the users information, name, address etc..  Then I've started to add things to this "profile" page which is always invoked via the "show" action.  The intent is to make this page more or less a dashboard (still in plumbing mode), so in addition to showing the user information I've started adding some relationships..  For example I know have added a list of all the tasks assigned to this user., I've also added a list of all his users skills...  These two relationships I've created in my "show" action and therefor quite easily added to the show view.

 

Now I've wanted to added a number of buttons all related to various search options..  For example "search_for_all_tasks", then "search_for_closed_tasks.. etc.  This is where I've lost a design pattern.  Thinking I simply create a search_action, have this method create relationship lists as instance attributes, redirect to "show" action then my show view I would have added an area to show the search results.

 

I'm beginning to think I need to move away from pulmbing concepts and figure out a better design pattern for such a simple thing like this.  I hate to create global variables, don't want it in session, so im missing something here.  Just thinking out loud, would I perhaps need to store the search list in the user model?  Or do I simply create the relationship in the view?  @tasks_search = Task.where(:status == "A"), but then I still need something to tell me what type of search it is, (what search button did he click)  status = A, or status = "I" or status = "C" etc...  Right now I see the only solution that can fit in my little brain is to store it in the user model....  If you have a better design pattern I'd be indebted to you for ever if you could splain it...

The way I got around this (not know if this is the correct way..) but anyway...  

 

I added a handful of search buttons to my screen that was originally renderes via the show page.

For each button, it links to a different method in my controller.

Each of these methods simply does one thing, render's the "show" page rather than link_to "show"

Then in by show view I have a series of if statements asking what action was just performed.  This will tell me what search option the user choose. 

 

It works, but perhaps there is a better way..






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users