Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old June 21st, 2006, 10:31 AM
SnowWhite's Avatar
SnowWhite SnowWhite is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 79 SnowWhite User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 25 m 46 sec
Reputation Power: 4
Ruby Newbie

Hi all,

Ruby and I are having a hard time bonding. I have a very basic question, and I hope that someone would be willing/able to assist me.

I have a simple web form where users may search by case number. I have my Controller set up to take a parameter, and query the DB with it. It appears that the parameter is getting set, but apparently the syntax I am using to query with is wrong. I am able to return a record when I hard-code a case number into the query statement, but not when I use a variable.

Now, if I were in php, this would be a breeze to solve... echo the value of the parameter to make sure it's getting set...echo the value of the query statement and make sure it's correct. Problem solved. Unfortunately, I don't know how to do even these simple error-checks in Ruby, and the books I have read are vague on this topic, to say the least.

Since Ruby is new here, I'll wait for a response before I start posting code snippetts of what syntax I've tried. I hope there are some Ruby humans out there that I can mingle with.

Thanks in advance!! ~Snow

Reply With Quote
  #2  
Old June 21st, 2006, 10:34 AM
LinuxPenguin's Avatar
LinuxPenguin LinuxPenguin is offline
fork while true;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2005
Location: England, UK
Posts: 5,535 LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)  Folding Points: 11590 Folding Title: Novice Folder
Time spent in forums: 1 Month 3 Weeks 1 Day 19 h 23 m 58 sec
Reputation Power: 999
This is specifically a rails question. please put that in the title in future.

I'm not a rails guy, but i'm very much a ruby guy. Can we see some code please?

Reply With Quote
  #3  
Old June 21st, 2006, 11:08 AM
SnowWhite's Avatar
SnowWhite SnowWhite is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 79 SnowWhite User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 25 m 46 sec
Reputation Power: 4
My apologies - as I said, I'm a newbie...

This page - search.rhtml - is where user enters case number to search by. Calls the 'list' method in the case_controller.rb.

Code:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<h1 align="center">Case Search</h1>
<h3 align="center"><font color="red">*</font>&nbsp;Indicates required field</h3>
</head>

<body>

<form action='list' method='post'>
  <table border="0" align="center" cellspacing="5" cellpadding="3" bgcolor="#DEBDDE">
    <tr>
      <td nowrap align="right">CASE ID:</td>
      <td><input type="text" name="case[case_id]"></td>
    </tr>
	<tr>
	  <td align="right">ENTRY DATE:</td>
      <td><input type="text" name="case[entry_date]"></td>
	</tr>
	<tr>
      <td align="right">NAME:</td>
	  <td><input type="text" name="case[l_name]" size="20">
	      <input type="text" name="case[f_name]" size="20"></td>
	</tr>
	<tr>
	  <td colspan="2" align="center"><input type="submit" name="submit" value="Query"></td>
	</tr>     
  </table>
</form>
</body>
</html>


case_controller.rb:

Code:
class CaseController < ApplicationController
  layout "cor_footer"
  scaffold :case

def list     

 @case = @params['case_id'] 
     
 @case_search = 
  Case.find(:all, 
              :conditions => ["case_id = @case",   @params])   
     

  end


Which should display results in list.rhtml:

Code:
<h2> Listing Search Results </h2>

<table border=1 cellpadding=5>

<tr>

<td width="20%"><p align="center"><i><b>Case ID</b></i></td>
<td width="60%"><p align="center"><i><b>Name</b></i></td>
<td width="20%"><p align="center"><i><b>Entry Date</b></i></td>

</tr>


<% if (@case == nil) || (@case == code.case_id) %>
<% @case_search.each do |code| %>

<tr>

<td>&nbsp;<%= code.case_id %></td>
<td>&nbsp;<%= code.l_name %></td>
<td>&nbsp;<%= code.entry_date %></td>

</tr>


<% end %>
<% end %>
</table>


As mentioned, I hit the table fine when I hard-code a value, like so:

Code:
@case_search = Case.find(:all, :conditions => "case_id = '28493'")




So, I'm guessing that the trouble lies here somewhere:

Code:
def list
  @case = @params['case_id']    
     
  @case_search = Case.find(:all, 
                   :conditions => ["case_id = @case", @params])
end


Thank you for helping!

Reply With Quote
  #4  
Old June 21st, 2006, 11:12 AM
LinuxPenguin's Avatar
LinuxPenguin LinuxPenguin is offline
fork while true;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2005
Location: England, UK
Posts: 5,535 LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)  Folding Points: 11590 Folding Title: Novice Folder
Time spent in forums: 1 Month 3 Weeks 1 Day 19 h 23 m 58 sec
Reputation Power: 999
You need to have it interpolate the values..

"case_id = '#@case'"

If it's not a package, global or class variable you need to do #{variable} so ruby knows.

edit: I also wrapped some quotes around it. I believe rails handles escaping for you, but i'm no rails guy.

Reply With Quote
  #5  
Old June 21st, 2006, 11:37 AM
SnowWhite's Avatar
SnowWhite SnowWhite is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 79 SnowWhite User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 25 m 46 sec
Reputation Power: 4
Thanks for the suggestion.

I tried this:

Code:
def list
     

     @case = @params['case_id']     
     @case_search = Case.find(:all, :conditions => "case_id = '#{@case}'")
                                                  
         

  end


Still no dice. I tried it with and without curlies. Perhaps my problem lies elsewhere. I'm only making the assumption that it is picking up my param value, because when I have a syntax error, it shows the value of case_id - but perhaps it is not passing it to the controller. Do you know of a way to echo the param value to the screen so I can see what I'm passing?

Reply With Quote
  #6  
Old June 21st, 2006, 11:41 AM
LinuxPenguin's Avatar
LinuxPenguin LinuxPenguin is offline
fork while true;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2005
Location: England, UK
Posts: 5,535 LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)  Folding Points: 11590 Folding Title: Novice Folder
Time spent in forums: 1 Month 3 Weeks 1 Day 19 h 23 m 58 sec
Reputation Power: 999
there's a flash command in rails i think, probably under another name

you could flash it.

Reply With Quote
  #7  
Old June 21st, 2006, 12:04 PM
SnowWhite's Avatar
SnowWhite SnowWhite is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 79 SnowWhite User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 25 m 46 sec
Reputation Power: 4
Ok, that helped alot! I now know that my param is not getting set correctly. SO - my next question for you is, do you know the difference between these syntax?

Code:
@case = @params['case_id']

@case = @params[:case_id]


I have seen them written both ways, and I'm thinking that they are interchangable, but I'm not sure?

Reply With Quote
  #8  
Old June 21st, 2006, 12:16 PM
LinuxPenguin's Avatar
LinuxPenguin LinuxPenguin is offline
fork while true;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2005
Location: England, UK
Posts: 5,535 LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)  Folding Points: 11590 Folding Title: Novice Folder
Time spent in forums: 1 Month 3 Weeks 1 Day 19 h 23 m 58 sec
Reputation Power: 999
They are interchangeable.

The first form is a string, you know that. The second form is a symbol. The idea is that if you use a string many times, you should convert it to a symbol. When ruby gets bytecode compilation down, it will save some memory by only storing it once.

Reply With Quote
  #9  
Old June 21st, 2006, 01:38 PM
SnowWhite's Avatar
SnowWhite SnowWhite is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 79 SnowWhite User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 25 m 46 sec
Reputation Power: 4
Well, it's still not getting the param into the variable. I put a flash on my list.rhtml page, and the var is empty.

Do you see anything else in my HTML or anywhere else that may throw a red flag at you? I'm running out of ideas!

Reply With Quote
  #10  
Old June 21st, 2006, 01:40 PM
LinuxPenguin's Avatar
LinuxPenguin LinuxPenguin is offline
fork while true;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2005
Location: England, UK
Posts: 5,535 LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)  Folding Points: 11590 Folding Title: Novice Folder
Time spent in forums: 1 Month 3 Weeks 1 Day 19 h 23 m 58 sec
Reputation Power: 999
Well i had presumed rails required you give those names... however i'm tempted to change them to static values at least as a test...

<input type="text" name="case_id">

Like i said, i'm not a rails guy, so sh!t may break.
Comments on this post
SnowWhite agrees!

Reply With Quote
  #11  
Old June 21st, 2006, 03:24 PM
SnowWhite's Avatar
SnowWhite SnowWhite is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 79 SnowWhite User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 25 m 46 sec
Reputation Power: 4
Well, believe or not, that worked! You're right, the reason I had named it that way was because when I was trying to load combo boxes in a different form, Rails wouldn't recognize the field unless it was associated with the table name...sheesh.
Wonder how that's going to work when I need to get combo box values back? Oh well - cross that bridge when I get to it I guess.

Thanks for your help and suggestions.
~Snow W.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Ruby Newbie


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump