Williams, Ali <[email protected]…> writes:
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
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
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| %>
Editing Case <%= edit.case_id %>
CAUSE INFORMATION
* Indicates required field
<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…