Hi, ? I''m a Ruby on Rails newbie.? I''ve been tasked with building our office''s first ROR application, and I am the only one working on it, so I am muddling through with a couple of books...? Moving along slowly but need some discussion time with some people who KNOW it and can maybe offer some answers to questions I haven''t found in my books. ? First - what is the fundamental difference between "render" and "redirect_to" when working with .rhtml forms in the Controller?? When is it appropriate to use these? ? Second - ?Why do I sometimes have to use a while loop to access my queried data in the .rhtml form?? Example: ? Controller-Edit Method @edit = Cause.find(:all, :conditions => "case_id = ''#{@caseid}''") ? View-Edit.rhtml <h2>Editing Case <%= @edit.case_id %> ? This method of data retrieval has worked for me in other similar forms, and I can''t figure out why it won''t work here.? The error it gives is that case_id is not a valid method, but it is a valid field in the table, so should work.? (case_id is the foreign key for the table) ? However, if I put the data retrieval inside a while loop, it displays the data, but gives me a routing error when I try to update. ? I have other questions as well, but I guess I''ll start here.? ANY AND ALL tips, advice, etc. would be greatly appreciated!! ? Thank you, ? Ali Williams Systems Developer II Washoe County IT 775 328-3720 Ali Williams Systems Developer II Washoe County IT 775 328-3720
> I''m a Ruby on Rails newbie. I''ve been tasked with building our office''s first ROR application, and I am the only one working on it, so I am muddling through with a couple of books... Moving along slowly but need some discussion time with some people who KNOW it and can maybe offer some answers to questions I haven''t found in my books.Grats, welcome to the wonderful world of Ralis. We''re all mad here. Or something like that; I can''t pretend to be _why, he really is mad.> First - what is the fundamental difference between "render" and "redirect_to" when working with .rhtml forms in the Controller? When is it appropriate to use these?As I understand it redirect_to issues a 302 and redirects the browser to a page, render on the other hand just renders whatever you say to render without the redirect. In my use I rarely use render directly (it''s the default call for every controller action so I actually use it all the time). The only time I call it explicitly is when I''m not sure if there''s going to be a redirect_to between me and the end of a function. So, essentially never unless I''ve not been writing tests for some reason. Just remember that `render function_name` is the default last call made by an action that hasn''t been redirected and you won''t have to worry about the difference.> Second - Why do I sometimes have to use a while loop to access my queried data in the .rhtml form? Example: > > Controller-Edit Method > @edit = Cause.find(:all, :conditions => "case_id = ''#{@caseid}''") > > View-Edit.rhtml > <h2>Editing Case <%= @edit.case_id %> > > This method of data retrieval has worked for me in other similar forms, and I can''t figure out why it won''t work here. The error it gives is that case_id is not a valid method, but it is a valid field in the table, so should work. (case_id is the foreign key for the table) > > However, if I put the data retrieval inside a while loop, it displays the data, but gives me a routing error when I try to update.This is a bit of a trick question without some more info, so I''ll try to answer all instances. 1. If you''re using belongs_to/has_one/has_many/habtm then you should just be doing @edit.case.id 2. @edit may be a hash, you''re using find(:all) which could return more than one object. You can find out by using the breakpointer or by having `<%= debug @edit %>` somewhere on the rhtml page. 3. @edit.case may be nil, in which case it would throw an error. If you send us the error messages we may be able to get to the root of your particular evil. Once again, welcome to the fold. If you aren''t already testing you should be; it''s not just for professionals anymore. :) Ta, Chuck Vose
Hi -- On Mon, 10 Jul 2006, Williams, Ali wrote:> Hi, > > I''m a Ruby on Rails newbie. I''ve been tasked with building our > office''s first ROR application, and I am the only one working on it, > so I am muddling through with a couple of books... Moving along > slowly but need some discussion time with some people who KNOW it > and can maybe offer some answers to questions I haven''t found in my > books. > > First - what is the fundamental difference between "render" and > "redirect_to" when working with .rhtml forms in the Controller? > When is it appropriate to use these?render lets you pick a non-default template for rendering. A typical use would be to render a "show" view for an item after updating it: def update @user = User.find(parmas[:id]) @user.update_attributes(params[:user]) render("user/show") end This does not execute the user/show action; it just "borrows" the user/show template, and fills it out according to what''s already been done in the update action. (Presumably that template would make use of the @user variable.) redirect_to invokes -- redirects the client to -- a new action. When the new action is executed, it''s basically as if the client went there in the first place. The new action has to do whatever''s necessary to populate its own template (or whichever one it renders).> Second - Why do I sometimes have to use a while loop to access my > queried data in the .rhtml form? Example: > > Controller-Edit Method > @edit = Cause.find(:all, :conditions => "case_id = ''#{@caseid}''") > > View-Edit.rhtml > <h2>Editing Case <%= @edit.case_id %> > > This method of data retrieval has worked for me in other similar > forms, and I can''t figure out why it won''t work here. The error it > gives is that case_id is not a valid method, but it is a valid field > in the table, so should work. (case_id is the foreign key for the > table) > > However, if I put the data retrieval inside a while loop, it > displays the data, but gives me a routing error when I try to > update.Doing a file(:all) operation will give you an array, and when you try to call case_id on an array, it won''t work. If you''ve initialized @caseid, you should be able to do: @edit = Case.find(@caseid) David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy http://www.manning.com/black => RUBY FOR RAILS, the Ruby book for Rails developers http://dablog.rubypal.com => D[avid ]A[. ]B[lack''s][ Web]log dblack@wobblini.net => me
On Monday, July 10, 2006, at 3:46 PM, Chuck Vose wrote:>> I''m a Ruby on Rails newbie. I''ve been tasked with building our >>office''s first ROR application, and I am the only one working on it, >>so I am muddling through with a couple of books... Moving along >>slowly but need some discussion time with some people who KNOW it >>and can maybe offer some answers to questions I haven''t found in my books. > >Grats, welcome to the wonderful world of Ralis. We''re all mad here. Or >something like that; I can''t pretend to be _why, he really is mad. > >> First - what is the fundamental difference between "render" and >>"redirect_to" when working with .rhtml forms in the Controller? When >>is it appropriate to use these? > >As I understand it redirect_to issues a 302 and redirects the browser >to a page, render on the other hand just renders whatever you say to >render without the redirect. In my use I rarely use render directly >(it''s the default call for every controller action so I actually use >it all the time). The only time I call it explicitly is when I''m not >sure if there''s going to be a redirect_to between me and the end of a >function. So, essentially never unless I''ve not been writing tests for >some reason. > >Just remember that `render function_name` is the default last call >made by an action that hasn''t been redirected and you won''t have to >worry about the difference. > >> Second - Why do I sometimes have to use a while loop to access my >>queried data in the .rhtml form? Example: >> >> Controller-Edit Method >> @edit = Cause.find(:all, :conditions => "case_id = ''#{@caseid}''") >> >> View-Edit.rhtml >> <h2>Editing Case <%= @edit.case_id %> >> >> This method of data retrieval has worked for me in other similar >>forms, and I can''t figure out why it won''t work here. The error it >>gives is that case_id is not a valid method, but it is a valid field >>in the table, so should work. (case_id is the foreign key for the table) >> >> However, if I put the data retrieval inside a while loop, it >>displays the data, but gives me a routing error when I try to update. > >This is a bit of a trick question without some more info, so I''ll try >to answer all instances. > >1. If you''re using belongs_to/has_one/has_many/habtm then you should >just be doing @edit.case.id >2. @edit may be a hash, you''re using find(:all) which could return >more than one object. You can find out by using the breakpointer or by >having `<%= debug @edit %>` somewhere on the rhtml page. >3. @edit.case may be nil, in which case it would throw an error. > >If you send us the error messages we may be able to get to the root of >your particular evil. > >Once again, welcome to the fold. If you aren''t already testing you >should be; it''s not just for professionals anymore. :) > >Ta, >Chuck Vose >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsI''d also be careful about using ''case'' as a model name. It''s also a ruby keyword, so it could cause all sorts of problems if you aren''t careful. _Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
Williams, Ali <AWilliams@...> writes:> > Hi, > > I''m a Ruby on Rails newbie. I''ve been tasked with building our office''s firstROR application, and I am the> only one working on it, so I am muddling through with a couple of books...Moving along slowly but need> some discussion time with some people who KNOW it and can maybe offer someanswers to questions I haven''t> found in my books. > > First - what is the fundamental difference between "render" and "redirect_to"when working with .rhtml> forms in the Controller? When is it appropriate to use these? > > Second - Why do I sometimes have to use a while loop to access my querieddata in the .rhtml form? Example:> > Controller-Edit Method > <at> edit = Cause.find(:all, :conditions => "case_id = ''#{ <at> caseid}''") > > View-Edit.rhtml > <h2>Editing Case <%= <at> edit.case_id %> > > This method of data retrieval has worked for me in other similar forms, and Ican''t figure out why it won''t> work here. The error it gives is that case_id is not a valid method, but itis a valid field in the table, so> should work. (case_id is the foreign key for the table) > > However, if I put the data retrieval inside a while loop, it displays thedata, but gives me a routing error> when I try to update. > > I have other questions as well, but I guess I''ll start here. ANY AND ALLtips, advice, etc. would be greatly appreciated!!> > Thank you, > > Ali Williams > Systems Developer II > Washoe County IT > 775 328-3720 > >Thanks for the cordial and speedy reply. It''s hard to find pleasant people to chat with in this realm. And plus you quoted my fave cartoon character, so I''ll be hanging around here! ;-) Ok, more specific code from Edit.rhtml: *********************************************************** <% @edit.each do |edit| %> <form action=''../update/<%= edit.id %>'' method=''post''> <input name="cause[id]" type="hidden" value="<%= edit.id %>" /> <h2>Editing Case <%= edit.case_id %> <h3>CAUSE INFORMATION</h3> <h3 align="center"> <font color="red">*</font> Indicates required field</h3> <table border="0" align="center" cellspacing="5" cellpadding="3" bgcolor="#DEBDDE"> <tr> <td align="right"> <font color="red">* </font>CAUSE OF DEATH:</td> <td width="30"><select name="cause[cod_id]"> <option></option> <% @cod.each do |code| %> <option value="<%= code.id %>" <%= '' selected'' if code.id == edit.cod_id %>> <%= code.description %> </option> <% end %> </select> </td> </tr> <tr> <td align="right">MANNER OF DEATH:</td> <td><select name="cause[mod_id]"> <option></option> <% @mod.each do |code| %> <option value="<%= code.id %>" <%= '' selected'' if code.id == edit.mod_id %>> <%= code.description %> </option> <% end %> </select> </td> </tr> <tr> <td nowrap align="right"> <font color="red">* </font>PRIMARY(a):</td> <td colspan="2"> <textarea wrap name="cause[cause_texta]" rows="6" cols="70"> <%= edit.cause_texta %></textarea></td> </tr> <br> <tr> <td colspan="4" align="center"> <input type="submit" name="submit" value="Update"> </td> </tr> </table> <% end %> ******************************************************************** As you can see, I''ve succumbed to putting my data retrieval in a while loop, so it now displays in the form correctly. However, when I try to update, I get routing error "Recognition failed for "/update/24" - which is the correct id for the row I''m trying to update, so I don''t see where it''s getting screwed up? The only thing I''m doing different from my other forms is that I''m calling the Show routine from a navigation bar, and it is handling the redirect based on whether the query finds anything in the table for the passed-in case_id: def show @caseid = @params[''case_id''] @cause = Cause.find(:all, :conditions => "case_id = ''#{@caseid}''") # if no Cause data for case_id, new; else, edit @len = @cause.length if @len == 0 redirect_to :action => ''new'', :case_id => @caseid else redirect_to :action => ''edit'', :case_id => @caseid end end Other than that, it is setup just like my other forms that WORK. Frustration...
ali wrote: However, when I try to update, I get routing error "Recognition failed for "/update/24" - which is the correct id for the row I''m trying to update, so I don''t see where it''s getting screwed up? ---------- can you put your update action from the controller here? i suspect that could be a part of the problem - - if the id (24) is the correct id you want to update in a habtam/has_many/has_somethign/etc relashinship for a certain table, and you are looking it up in a different table (i.e, the second party in the relashinship), that may cause problems. also, not less importantly, i strongly suggest you use <%= start_form_tag :action => ''update'' %> rather than <form action=''../update/<%= edit.id %>'' method=''post''> ...good plausible reason for the routing error you''re getting ((Recognition failed for "/update/24)) if the action itself does exist in the controller... anyway, i hope it helps, and welcome to ror, shai -- Posted via http://www.ruby-forum.com/.
shai <shaiguitar@...> writes:> > ali wrote: > However, when I try to update, I > get routing error "Recognition failed for "/update/24" - which is the > correct id for the row I''m trying to update, so I don''t see where it''s > getting screwed up? > > ---------- > > can you put your update action from the controller here? i suspect that > could be a part of the problem - - if the id (24) is the correct id you > want to update in a habtam/has_many/has_somethign/etc relashinship for a > certain table, and you are looking it up in a different table (i.e, the > second party in the relashinship), that may cause problems. > > also, not less importantly, i strongly suggest you use > <%= start_form_tag :action => ''update'' %> rather than > <form action=''../update/?%= edit.id %>'' method=''post''> > ...good plausible reason for the routing error you''re getting > ((Recognition failed for "/update/24)) if the action itself does exist > in the controller... > > anyway, i hope it helps, > and welcome to ror, > > shai >ThankYou ThankYou for all your suggestions - they have been very helpful! My next question is regarding sessions. Does ROR support session variables, or just sessions in general? I haven''t found anything on session variables specifically. In my "layout", I have created a navigation bar that will be displayed on every page. I want each link to link to the "show" method for that controller, and pass in the case_id. Users work with the same case_id until the brower is closed, or they do a search for a different case, so the case_id needs to stay set and displayed on every form. Should I use a session for this, or attempt to keep passing through GET for every form action? Thanks again! ~Ali
On 7/11/06, Williams, Ali <AWilliams@washoecounty.us> wrote:> Hi, > > I''m a Ruby on Rails newbie. I''ve been tasked with building our office''s first ROR application, and I am the only one working on it, so I am muddling through with a couple of books... Moving along slowly but need some discussion time with some people who KNOW it and can maybe offer some answers to questions I haven''t found in my books. > > First - what is the fundamental difference between "render" and "redirect_to" when working with .rhtml forms in the Controller? When is it appropriate to use these? >"redirect_to" is generally used after a post or some other action you don''t want repeated if the user should refresh the page in their browser. Isak
On 7/11/06, Isak Hansen <isak.hansen@gmail.com> wrote:> On 7/11/06, Williams, Ali <AWilliams@washoecounty.us> wrote: > > Hi, > > > > I''m a Ruby on Rails newbie. I''ve been tasked with building our office''s first ROR application, and I am the only one working on it, so I am muddling through with a couple of books... Moving along slowly but need some discussion time with some people who KNOW it and can maybe offer some answers to questions I haven''t found in my books. > > > > First - what is the fundamental difference between "render" and "redirect_to" when working with .rhtml forms in the Controller? When is it appropriate to use these? > > > > "redirect_to" is generally used after a post or some other action you > don''t want repeated if the user should refresh the page in their > browser. >It''s not a foolproof method of preventing the same action to be called twice, however.. Isak
> > can you put your update action from the controller here? i suspect that > > could be a part of the problem - - if the id (24) is the correct id you > > want to update in a habtam/has_many/has_somethign/etc relashinship for a > > certain table, and you are looking it up in a different table (i.e, the > > second party in the relashinship), that may cause problems. > > > > also, not less importantly, i strongly suggest you use > > <%= start_form_tag :action => ''update'' %> rather than > > <form action=''../update/?%= edit.id %>'' method=''post''> > > ...good plausible reason for the routing error you''re getting > > ((Recognition failed for "/update/24)) if the action itself does exist > > in the controller... > > > > anyway, i hope it helps, > > and welcome to ror, > > > > shai > > > > > ThankYou ThankYou for all your suggestions - they have been very helpful! > > My next question is regarding sessions. Does ROR support session variables, or > just sessions in general? I haven''t found anything on session variables > specifically. In my "layout", I have created a navigation bar that will be > displayed on every page. I want each link to link to the "show" method for that > controller, and pass in the case_id. Users work with the same case_id until the > brower is closed, or they do a search for a different case, so the case_id needs > to stay set and displayed on every form. Should I use a session for this, or > attempt to keep passing through GET for every form action?Hehe, I would say that a variable that exists for the duration of a users session should probably be a session var. But that''s just me, I could be wrong. :P @session[:monkey] = "I like monkeys... Maybe too much." or @flash[:gordon] = :space_ship_captain which will persist for one redirection (most useful for doing flash[:notice] or flash[:error] when an action triggers an error and then redirects to some other page) Have you picked up Agile Web Development With Rails yet? It''s pretty important, most of these questions have been answered in much more detail in that book (and the authors are good kids). Cheers, Chuck Vose