Hello I do not understand how to use instance variable properly with partial views, I am hoping someone here can enlighten me. For example class MainController < ApplicationController def index @item_list = Item.find_all_item end def detail_display @current_selected = @item= Item.find(params[:id]) redirect_to :action => :index end end detail_display is invoked when the user clicks on an item in the list. The variable @current_selected is not available to the partial view invoked when the index is redirected to. How can I remedy this? Thank you rube_noob -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 1, 8:48 pm, Rube Noob <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello > > I do not understand how to use instance variable properly with partial > views, I am hoping someone here can enlighten me. For example > > class MainController < ApplicationController > def index > @item_list = Item.find_all_item > end > > def detail_display > @current_selected = @item= Item.find(params[:id]) > redirect_to :action => :index > end > end > > detail_display is invoked when the user clicks on an item in the list. > The variable @current_selected is not available to the partial view > invoked when the index is redirected to. How can I remedy this? >The key thing you need to understand here is that this is an http redirect, ie a separate request: by the time the index action is rendered the controller object that had the @current_selected instance variable has been destroyed and your request is being handled by a completely fresh controller object. If the index action needs to know what the currently selected object is then you need to pass a parameter to it telling it this. In this light, I''m not sure what the point of the detail_display action is as it seems to be a complete no- op. Fred> Thank you > > rube_noob > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Fred, Thanks for your response, Basically what I am trying to accomplish is have to partial views in the index page. One for the list of items and one for the details of the selected item. In the future I hope to add new functionality to the index page and I hope that the index method will not have to deal with all the different actions that I add. I understand how this is a new HTTP request and that the @current_selected variable is lost. Is there any way I can allow the view to see this variable without involving the index method? rube_noob Frederick Cheung wrote:> On Mar 1, 8:48�pm, Rube Noob <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> � � def detail_display >> � � � � @current_selected = @item= Item.find(params[:id]) >> � � � � redirect_to :action => :index >> � � end >> end >> >> detail_display is invoked when the user clicks on an item in the list. >> The variable @current_selected is not available to the partial view >> invoked when the index is redirected to. How can I remedy this? >> > The key thing you need to understand here is that this is an http > redirect, ie a separate request: by the time the index action is > rendered the controller object that had the @current_selected instance > variable has been destroyed and your request is being handled by a > completely fresh controller object. If the index action needs to know > what the currently selected object is then you need to pass a > parameter to it telling it this. In this light, I''m not sure what the > point of the detail_display action is as it seems to be a complete no- > op. > > Fred-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 1, 9:12 pm, Rube Noob <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi Fred, > > Thanks for your response, > > Basically what I am trying to accomplish is have to partial views in the > index page. One for the list of items and one for the details of the > selected item. In the future I hope to add new functionality to the > index page and I hope that the index method will not have to deal with > all the different actions that I add. > > I understand how this is a new HTTP request and that the > @current_selected variable is lost. Is there any way I can allow the > view to see this variable without involving the index method? >Nope, you''d have to pass a parameter to the index action (although whether manipulation of that parameter happens in the index action itself or in a before_filter is up to you) Fred> rube_noob > > > > Frederick Cheung wrote: > > On Mar 1, 8:48 pm, Rube Noob <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> def detail_display > >> @current_selected = @item= Item.find(params[:id]) > >> redirect_to :action => :index > >> end > >> end > > >> detail_display is invoked when the user clicks on an item in the list. > >> The variable @current_selected is not available to the partial view > >> invoked when the index is redirected to. How can I remedy this? > > > The key thing you need to understand here is that this is an http > > redirect, ie a separate request: by the time the index action is > > rendered the controller object that had the @current_selected instance > > variable has been destroyed and your request is being handled by a > > completely fresh controller object. If the index action needs to know > > what the currently selected object is then you need to pass a > > parameter to it telling it this. In this light, I''m not sure what the > > point of the detail_display action is as it seems to be a complete no- > > op. > > > Fred > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---