Hello all. I''m a noob at Ruby on Rails, only started learning a few days ago, but I''m loving it so far. I do have a few years experience with PHP, ASP, MySQL, etc., so I''m not *totally* clueless, though. :) Anyway, I''m having trouble with something that should be fairly trivial: I have an ''if'' statement that is supposed to print out a table row if a vriable is either nil, or matches another specific value (like the Rolling with Rails tutorial example). Unfortunately, it only works halfway. It will print the table rows if the variable is nil, but if the variable has any valu in it at all, it prints nothing, even when it matches the other value. I''ve been banging my head against this for about an hour now, and I think it''s one of those things where I can''t see it because it''s too simple, so I''m hoping that maybe one of you can tell me what''s wrong by looking at the code? Here''s the code I''m using: --------------------------------------------- <% @carttransactions.each do |carttransaction| %> <%= @cartno %><%= carttransaction.cart.id %> <% if (@cartno == nil ) || (@cartno == carttransaction.cart.id ) %> <tr> <% for column in Carttransaction.content_columns %> <td><%=h carttransaction.send(column.name) %></td> <% end %> <td><%= link_to ''Show'', :action => ''show'', :id => carttransaction %></td> <td><%= link_to ''Edit'', :action => ''edit'', :id => carttransaction %></td> <td><%= link_to ''Destroy'', { :action => ''destroy'', :id => carttransaction }, :confirm => ''Are you sure?'' %></td> </tr> <% end %> <% end %> ---------------------------------------------- When I access the page with ''http://localhost:3000/carttransaction/list'', it displays the table rows perfectly. However, if I try ''http://localhost:3000/carttransaction/list?cartno=1'', it displays none of the rows, not even the ones where ''carttransaction.cart.id'' equals 1. You''ll notice the line ''<%= @cartno %><%= carttransaction.cart.id %>'' up there, I put that in to make sure that, indeed, both vars were there and their values matched, and the page prints them both out and shows that @cartno is, indeed, receiving the value from the URL, and that the other one also has its value.. so, any ideas at all? I''m stumped. -- Posted via http://www.ruby-forum.com/.
I must admit I skimmed through your email, but it looks like you should maybe look into using a partial here. Will make your code much cleaner and easier to read. <table> <%= render(:partial => "transaction", :collection => @carttransactions %> <table> _transaction.rhtml ~~~~~~~~~~~~~~~~~~~~ <tr> <td><!-- DISPLAY FOR AN INDIVIDUAL TRANSACTION HERE --></td> </tr> Again this is just a general rails tip... ~ Ben On 3/11/06, Daniel Rodriguez <katsushiro@gmail.com> wrote:> Hello all. I''m a noob at Ruby on Rails, only started learning a few days > ago, but I''m loving it so far. I do have a few years experience with > PHP, ASP, MySQL, etc., so I''m not *totally* clueless, though. :) > > Anyway, I''m having trouble with something that should be fairly trivial: > I have an ''if'' statement that is supposed to print out a table row if a > vriable is either nil, or matches another specific value (like the > Rolling with Rails tutorial example). Unfortunately, it only works > halfway. It will print the table rows if the variable is nil, but if the > variable has any valu in it at all, it prints nothing, even when it > matches the other value. I''ve been banging my head against this for > about an hour now, and I think it''s one of those things where I can''t > see it because it''s too simple, so I''m hoping that maybe one of you can > tell me what''s wrong by looking at the code? Here''s the code I''m using: > > --------------------------------------------- > <% @carttransactions.each do |carttransaction| %> > <%= @cartno %><%= carttransaction.cart.id %> > <% if (@cartno == nil ) || (@cartno == carttransaction.cart.id ) %> > <tr> > <% for column in Carttransaction.content_columns %> > <td><%=h carttransaction.send(column.name) %></td> > <% end %> > <td><%= link_to ''Show'', :action => ''show'', :id => carttransaction > %></td> > <td><%= link_to ''Edit'', :action => ''edit'', :id => carttransaction > %></td> > <td><%= link_to ''Destroy'', { :action => ''destroy'', :id => > carttransaction }, :confirm => ''Are you sure?'' %></td> > </tr> > <% end %> > <% end %> > ---------------------------------------------- > > When I access the page with > ''http://localhost:3000/carttransaction/list'', it displays the table rows > perfectly. However, if I try > ''http://localhost:3000/carttransaction/list?cartno=1'', it displays none > of the rows, not even the ones where ''carttransaction.cart.id'' equals 1. > You''ll notice the line > ''<%= @cartno %><%= carttransaction.cart.id %>'' > up there, I put that in to make sure that, indeed, both vars were there > and their values matched, and the page prints them both out and shows > that @cartno is, indeed, receiving the value from the URL, and that the > other one also has its value.. so, any ideas at all? I''m stumped. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ben Reubenstein http://www.benr75.com
Thanks for the tip, Ben, I''ll look into it; I can see several places in my app where the use of a partial would clean things up considerably. However, this doesn''t address my initial problem: that the ''if'' statement doesn''t seem to be evaluating right. It runs fine if the first condition in the ''or'' pair is true, but doesn''t run if the second condition is true, even though it should. Any ideas? -Daniel Ben Reubenstein wrote:> I must admit I skimmed through your email, but it looks like you > should maybe look into using a partial here. Will make your code much > cleaner and easier to read. > > <table> > <%= render(:partial => "transaction", :collection => @carttransactions > %> > <table> > > _transaction.rhtml > ~~~~~~~~~~~~~~~~~~~~ > > <tr> > <td><!-- DISPLAY FOR AN INDIVIDUAL TRANSACTION HERE --></td> > </tr> > > Again this is just a general rails tip... > > ~ Ben-- Posted via http://www.ruby-forum.com/.
Hi Daniel, I''m pretty new to this too, but I think the problem may be in the second line. <%= @cartno %><%= carttransaction.cart.id %> should be... <%= @cartno = carttransaction.cart.id %> Or maybe not ;-) Let us know. Best regards, Bill ----- Original Message ----- From: "Daniel Rodriguez" <katsushiro@gmail.com> To: <rails@lists.rubyonrails.org> Sent: 2006-03-11 5:30 PM Subject: [Rails] Noob needs help with ''if'' block> Hello all. I''m a noob at Ruby on Rails, only started learning a few days > ago, but I''m loving it so far. I do have a few years experience with > PHP, ASP, MySQL, etc., so I''m not *totally* clueless, though. :) > > Anyway, I''m having trouble with something that should be fairly trivial: > I have an ''if'' statement that is supposed to print out a table row if a > vriable is either nil, or matches another specific value (like the > Rolling with Rails tutorial example). Unfortunately, it only works > halfway. It will print the table rows if the variable is nil, but if the > variable has any valu in it at all, it prints nothing, even when it > matches the other value. I''ve been banging my head against this for > about an hour now, and I think it''s one of those things where I can''t > see it because it''s too simple, so I''m hoping that maybe one of you can > tell me what''s wrong by looking at the code? Here''s the code I''m using: > > --------------------------------------------- > <% @carttransactions.each do |carttransaction| %> > <%= @cartno %><%= carttransaction.cart.id %> > <% if (@cartno == nil ) || (@cartno == carttransaction.cart.id ) %> > <tr> > <% for column in Carttransaction.content_columns %> > <td><%=h carttransaction.send(column.name) %></td> > <% end %> > <td><%= link_to ''Show'', :action => ''show'', :id => carttransaction > %></td> > <td><%= link_to ''Edit'', :action => ''edit'', :id => carttransaction > %></td> > <td><%= link_to ''Destroy'', { :action => ''destroy'', :id => > carttransaction }, :confirm => ''Are you sure?'' %></td> > </tr> > <% end %> > <% end %> > ---------------------------------------------- > > When I access the page with > ''http://localhost:3000/carttransaction/list'', it displays the table rows > perfectly. However, if I try > ''http://localhost:3000/carttransaction/list?cartno=1'', it displays none > of the rows, not even the ones where ''carttransaction.cart.id'' equals 1. > You''ll notice the line > ''<%= @cartno %><%= carttransaction.cart.id %>'' > up there, I put that in to make sure that, indeed, both vars were there > and their values matched, and the page prints them both out and shows > that @cartno is, indeed, receiving the value from the URL, and that the > other one also has its value.. so, any ideas at all? I''m stumped. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Daniel, I suggest to change <% if (@cartno == nil ) || (@cartno == carttransaction.cart.id ) %> for <% if (@cartno == carttransaction.cart.id) || !@cartno %> And then try again This approach depends on you Database schema... if most of your fields are null, then I will suggest to change !@cartno to the other side of the ||. Normally you don''t want to have many null values on your database (if you have many null values means that probably your schema needs to be changed unless you have designed that way ;) ) Anyway, I don''t know if this solve your problem but give at least give you a hint on other places to looks at :) Cheers, Rafa Bill Walton wrote:> Hi Daniel, > > I''m pretty new to this too, but I think the problem may be in the second > line. > > <%= @cartno %><%= carttransaction.cart.id %> > > should be... > > <%= @cartno = carttransaction.cart.id %> > > Or maybe not ;-) Let us know. > > Best regards, > Bill > > > ----- Original Message ----- > From: "Daniel Rodriguez" <katsushiro@gmail.com> > To: <rails@lists.rubyonrails.org> > Sent: 2006-03-11 5:30 PM > Subject: [Rails] Noob needs help with ''if'' block-- Posted via http://www.ruby-forum.com/.
Thanks for the help so far, but still no luck: Bill: Actually, that line is there simply to print out the values of @cartno and carttransaction.cart.id so I can see that they have the right values, I''m going to delete that line before I go into production. I''ve tried running the app without that line as well, doesn''t make a difference. Rath: I tried changing that line of code as you suggested, but it still does the same thing. And the way you suggested is fine the databse doesn''t have many null values, @cartno comes in from an URL parameter. Any more ideas? I''m gonna keep trying to figure it out as well, if I manage to figure out what''s wrong, I''ll post it here. -Daniel Rath wrote:> Daniel, > > I suggest to change > <% if (@cartno == nil ) || (@cartno == carttransaction.cart.id ) %> > for > <% if (@cartno == carttransaction.cart.id) || !@cartno %> > And then try again > > This approach depends on you Database schema... if most of your fields > are null, then I will suggest to change !@cartno to the other side of > the ||. Normally you don''t want to have many null values on your > database (if you have many null values means that probably your schema > needs to be changed unless you have designed that way ;) ) > > Anyway, I don''t know if this solve your problem but give at least give > you a hint on other places to looks at :) > > Cheers, > > Rafa > > Bill Walton wrote: >> Hi Daniel, >> >> I''m pretty new to this too, but I think the problem may be in the second >> line. >> >> <%= @cartno %><%= carttransaction.cart.id %> >> >> should be... >> >> <%= @cartno = carttransaction.cart.id %> >> >> Or maybe not ;-) Let us know. >> >> Best regards, >> Bill >> >> >> ----- Original Message ----- >> From: "Daniel Rodriguez" <katsushiro@gmail.com> >> To: <rails@lists.rubyonrails.org> >> Sent: 2006-03-11 5:30 PM >> Subject: [Rails] Noob needs help with ''if'' block-- Posted via http://www.ruby-forum.com/.
Have you tried to use the "console" script and check it from there? open a promt on the root directory and type root> ruby script/console>>You can debug your program from there... it isn''t perfect but sometimes it is really useful to use this command :) Cheers, Rafa -- Posted via http://www.ruby-forum.com/.
On 3/11/06, Daniel Rodriguez <katsushiro@gmail.com> wrote:> Hello all. I''m a noob at Ruby on Rails, only started learning a few days > ago, but I''m loving it so far. I do have a few years experience with > PHP, ASP, MySQL, etc., so I''m not *totally* clueless, though. :) > > Anyway, I''m having trouble with something that should be fairly trivial: > I have an ''if'' statement that is supposed to print out a table row if a > vriable is either nil, or matches another specific value (like the > Rolling with Rails tutorial example). Unfortunately, it only works > halfway. It will print the table rows if the variable is nil, but if the > variable has any valu in it at all, it prints nothing, even when it > matches the other value. I''ve been banging my head against this for > about an hour now, and I think it''s one of those things where I can''t > see it because it''s too simple, so I''m hoping that maybe one of you can > tell me what''s wrong by looking at the code? Here''s the code I''m using: > > --------------------------------------------- > <% @carttransactions.each do |carttransaction| %> > <%= @cartno %><%= carttransaction.cart.id %>Try wrapping those. Maybe they only ''look'' equal. <%= ''|'' + @cartno.to_s + ''|'' %><%= ''|'' + carttransaction.cart.id.to_s + ''|'' %> <% if (@cartno == nil ) || (@cartno == carttransaction.cart.id ) %>> <tr> > <% for column in Carttransaction.content_columns %> > <td><%=h carttransaction.send(column.name) %></td> > <% end %> > <td><%= link_to ''Show'', :action => ''show'', :id => carttransaction > %></td> > <td><%= link_to ''Edit'', :action => ''edit'', :id => carttransaction > %></td> > <td><%= link_to ''Destroy'', { :action => ''destroy'', :id => > carttransaction }, :confirm => ''Are you sure?'' %></td> > </tr> > <% end %> > <% end %> > ---------------------------------------------- > > When I access the page with > ''http://localhost:3000/carttransaction/list'', it displays the table rows > perfectly. However, if I try > ''http://localhost:3000/carttransaction/list?cartno=1'', it displays none > of the rows, not even the ones where ''carttransaction.cart.id'' equals 1. > You''ll notice the line > ''<%= @cartno %><%= carttransaction.cart.id %>'' > up there, I put that in to make sure that, indeed, both vars were there > and their values matched, and the page prints them both out and shows > that @cartno is, indeed, receiving the value from the URL, and that the > other one also has its value.. so, any ideas at all? I''m stumped. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Bill Guindon (aka aGorilla) The best answer to most questions is "it depends".
Well, finaly found the problem! They ''looked'' equal, but apparently, it was seeing one var as a string and another as something else. I checked in the database, and both the ''id'' field in table ''carts'' and the ''cart_id'' field in table ''carttransactions'' are set as int(11), but, apparently, at some point one of the variables I''m using for comparison stops being an int. (My guess is ''@cartno'', since it comes as an URL parameter, is read as a string, while ''carttransaction.cart.id'', coming straight from the database, is read as an int). I fixed it by adding .to_s to both variables in the comparison (and doing some changes as suggested earlier), so that <% if (@cartno == nil ) || (@cartno == carttransaction.cart.id ) %> becomes, instead: <% if (@cartno.to_s == carttransaction.cart.id.to_s) || !@cartno %> Now it works flawlessly. :) Thanks muchly for everyone''s help and suggestions, and especially to Bill Guindon for pointing me in the right direction. -Daniel Bill Guindon wrote:> On 3/11/06, Daniel Rodriguez <katsushiro@gmail.com> wrote: >> matches the other value. I''ve been banging my head against this for >> about an hour now, and I think it''s one of those things where I can''t >> see it because it''s too simple, so I''m hoping that maybe one of you can >> tell me what''s wrong by looking at the code? Here''s the code I''m using: >> >> --------------------------------------------- >> <% @carttransactions.each do |carttransaction| %> >> <%= @cartno %><%= carttransaction.cart.id %> > > Try wrapping those. Maybe they only ''look'' equal. > > <%= ''|'' + @cartno.to_s + ''|'' %><%= ''|'' + carttransaction.cart.id.to_s + > ''|'' %> > > <% if (@cartno == nil ) || (@cartno == carttransaction.cart.id ) %> >> </tr> >> ''<%= @cartno %><%= carttransaction.cart.id %>'' >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > -- > Bill Guindon (aka aGorilla) > The best answer to most questions is "it depends".-- Posted via http://www.ruby-forum.com/.
Daniel, I''d suggest, in the interests of making your code a little less PHP- ish and a bit more Ruby-ish: <% if @cartno.to_i == carttransaction.cart.id or @cartno.nil? %> Cheers, Pete Yandell On 12/03/2006, at 1:26 PM, Daniel Rodriguez wrote:> Well, finaly found the problem! > > They ''looked'' equal, but apparently, it was seeing one var as a string > and another as something else. I checked in the database, and both the > ''id'' field in table ''carts'' and the ''cart_id'' field in table > ''carttransactions'' are set as int(11), but, apparently, at some point > one of the variables I''m using for comparison stops being an int. (My > guess is ''@cartno'', since it comes as an URL parameter, is read as a > string, while ''carttransaction.cart.id'', coming straight from the > database, is read as an int). > > I fixed it by adding .to_s to both variables in the comparison (and > doing some changes as suggested earlier), so that > <% if (@cartno == nil ) || (@cartno == carttransaction.cart.id ) %> > > becomes, instead: > > <% if (@cartno.to_s == carttransaction.cart.id.to_s) || !@cartno %> > > Now it works flawlessly. :) > > Thanks muchly for everyone''s help and suggestions, and especially to > Bill Guindon for pointing me in the right direction. > > -Daniel > > Bill Guindon wrote: >> On 3/11/06, Daniel Rodriguez <katsushiro@gmail.com> wrote: >>> matches the other value. I''ve been banging my head against this for >>> about an hour now, and I think it''s one of those things where I >>> can''t >>> see it because it''s too simple, so I''m hoping that maybe one of >>> you can >>> tell me what''s wrong by looking at the code? Here''s the code I''m >>> using: >>> >>> --------------------------------------------- >>> <% @carttransactions.each do |carttransaction| %> >>> <%= @cartno %><%= carttransaction.cart.id %> >> >> Try wrapping those. Maybe they only ''look'' equal. >> >> <%= ''|'' + @cartno.to_s + ''|'' %><%= ''|'' + >> carttransaction.cart.id.to_s + >> ''|'' %> >> >> <% if (@cartno == nil ) || (@cartno == carttransaction.cart.id ) %> >>> </tr> >>> ''<%= @cartno %><%= carttransaction.cart.id %>'' >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >> >> >> -- >> Bill Guindon (aka aGorilla) >> The best answer to most questions is "it depends". > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Heh, is it that obvious that I''ve mostly worked in PHP till now? :) Thanks for the tip, I''ll try that out, looks good! -Daniel Pete Yandell wrote:> Daniel, > > I''d suggest, in the interests of making your code a little less PHP- > ish and a bit more Ruby-ish: > > <% if @cartno.to_i == carttransaction.cart.id or @cartno.nil? %> > > Cheers, > > Pete Yandell-- Posted via http://www.ruby-forum.com/.
If I read this thread correctly, one of the terms in the comparison came from "a URL parameter." Because params and other querystring data contain no metadata, they are always represented as strings initially. One good way to see what''s going on is: tail -f log/development.log as you pop around in the browser, you will have all the information you need, including the entire params hash for each request. That would probably have given you the clue you needed. Two other ways I find handy to debug things like this are: <%= debug(var) %> and logger.debug "var[#{var}] isa: #{var.class}" Hope this helps in the future. Daniel Rodriguez wrote:> Heh, is it that obvious that I''ve mostly worked in PHP till now? :) > > Thanks for the tip, I''ll try that out, looks good! > > -Daniel > > Pete Yandell wrote: >> Daniel, >> >> I''d suggest, in the interests of making your code a little less PHP- >> ish and a bit more Ruby-ish: >> >> <% if @cartno.to_i == carttransaction.cart.id or @cartno.nil? %> >> >> Cheers, >> >> Pete Yandell-- Posted via http://www.ruby-forum.com/.