I''d like to set a value somewhere to let the view know if it is in "add new" mode or "Update" mode. Depending on the mode, one line of text in the view is to be presented differently (e.g. <% if updateMode == true %>). This brings up a couple of questions: Where is the best place to store that value? What is the proper way to set the value in the controller and to retrieve it in the view? Thanks in advance ---Michael -- 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-22 19:59 UTC
Re: Setting a variable for view
Hi -- On Sun, 22 Oct 2006, Michael Satterwhite wrote:> > I''d like to set a value somewhere to let the view know if it is in "add > new" mode or "Update" mode. Depending on the mode, one line of text in > the view is to be presented differently (e.g. <% if updateMode == true > %>). > > This brings up a couple of questions: > Where is the best place to store that value? > What is the proper way to set the value in the controller and to > retrieve it in the view?Instance variables set in the controller are visible in the view. So you would want to do: # Controller: def my_action @update_mode = true ... end # View: <% if @update_mode %> ... <% end %> (Note that you don''t need "== true".) Mind you, if you find yourself doing this a lot, it''s probably a sign that you should reorganize your actions and views, perhaps by splitting off an update sequence. I''ve run into situations like that with @admin_session and other such flags: I realize eventually that I''m really trying to squeeze two separate use cases into one view, where it might be better, and scale better, to have a separate suite of admin tools. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Michael, Michael Satterwhite wrote:> > I''d like to set a value somewhere to let the view > know if it is in "add new" mode or "Update" mode. > Depending on the mode, one line of text in the view > is to be presented differently (e.g. <% if updateMode == true > %>).The simplest approach might be to set an instance variable (e.g., @updateMode) in your controller and then do a check (e.g.,<% if !@updateMode.nil? %>) in the view to figure out where you are. hth, Bill --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
If this specific issue as what you''re addressing, how about: if @my_model_object.new_record? # do new code else # do edit code end of if you are just changing the button names, use something like: <%= submit_button(my_model_object.new_record? ? ''add new'' : ''update now'') %> Yes? On Oct 22, 2006, at 12:53 PM, Michael Satterwhite wrote:> > I''d like to set a value somewhere to let the view know if it is in > "add > new" mode or "Update" mode. Depending on the mode, one line of text in > the view is to be presented differently (e.g. <% if updateMode == true > %>). > > This brings up a couple of questions: > Where is the best place to store that value? > What is the proper way to set the value in the controller and to > retrieve it in the view? > > Thanks in advance > ---Michael > > -- > 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 -~----------~----~----~----~------~----~------~--~---