# ROR questions

**URL:** <https://www.ruby-forum.com/t/ror-questions/97871>\
**Category:** Rails\
**Created:** [July 11, 2006, 12:29am UTC](https://www.ruby-forum.com/t/ror-questions/97871 "2006-07-11T00:29:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![WilliamsS\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ce7236/32.png) [@WilliamsS\_Ali](https://www.ruby-forum.com/u/WilliamsS_Ali)\
**Post date:** [July 11, 2006, 12:29am UTC](https://www.ruby-forum.com/t/ror-questions/97871/1 "2006-07-11T00:29:10Z")

</div>

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

Ali Williams  
Systems Developer II  
Washoe County IT  
775 328-3720

---

<div class="post-metadata">

**Author:** ![WilliamsS\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ce7236/32.png) [@WilliamsS\_Ali](https://www.ruby-forum.com/u/WilliamsS_Ali)\
**Post date:** [July 11, 2006, 12:48am UTC](https://www.ruby-forum.com/t/ror-questions/97871/2 "2006-07-11T00:48:57Z")

</div>

> 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.

> 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 V.

---

<div class="post-metadata">

**Author:** ![WilliamsS\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ce7236/32.png) [@WilliamsS\_Ali](https://www.ruby-forum.com/u/WilliamsS_Ali)\
**Post date:** [July 11, 2006, 1:04am UTC](https://www.ruby-forum.com/t/ror-questions/97871/3 "2006-07-11T01:04:46Z")

</div>

On Monday, July 10, 2006, at 3:46 PM, Chuck V. wrote:

> > “redirect\_to” when working with .rhtml forms in the Controller? When
> 
> > View-Edit.rhtml  
> > This is a bit of a trick question without some more info, so I’ll try  
> > 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 V.
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

I’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

---

<div class="post-metadata">

**Author:** ![WilliamsS\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ce7236/32.png) [@WilliamsS\_Ali](https://www.ruby-forum.com/u/WilliamsS_Ali)\
**Post date:** [July 11, 2006, 8:13am UTC](https://www.ruby-forum.com/t/ror-questions/97871/4 "2006-07-11T08:13:41Z")

</div>

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

...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

---

<div class="post-metadata">

**Author:** ![WilliamsS\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ce7236/32.png) [@WilliamsS\_Ali](https://www.ruby-forum.com/u/WilliamsS_Ali)\
**Post date:** [July 11, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/ror-questions/97871/5 "2006-07-11T18:25:31Z")

</div>

shai \<shaiguitar@…\> writes:

> could be a part of the problem - - if the id (24) is the correct id you
> 
> 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

---

<div class="post-metadata">

**Author:** ![WilliamsS\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ce7236/32.png) [@WilliamsS\_Ali](https://www.ruby-forum.com/u/WilliamsS_Ali)\
**Post date:** [July 11, 2006, 1:17am UTC](https://www.ruby-forum.com/t/ror-questions/97871/6 "2006-07-11T01:17:11Z")

</div>

Williams, Ali \<AWilliams@…\> 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

### \*&nbsp;Indicates required field

```
<table border="0" align="center" cellspacing="5"
          cellpadding="3" bgcolor="#DEBDDE">
<tr>
  <td align="right">
       <font color="red">*&nbsp;</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">*&nbsp;</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…

---

<div class="post-metadata">

**Author:** ![WilliamsS\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ce7236/32.png) [@WilliamsS\_Ali](https://www.ruby-forum.com/u/WilliamsS_Ali)\
**Post date:** [July 11, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/ror-questions/97871/7 "2006-07-11T23:31:11Z")

</div>

On 7/11/06, Isak H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> browser.

It’s not a foolproof method of preventing the same action to be called  
twice, however…

Isak

---

<div class="post-metadata">

**Author:** ![WilliamsS\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ce7236/32.png) [@WilliamsS\_Ali](https://www.ruby-forum.com/u/WilliamsS_Ali)\
**Post date:** [July 11, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/ror-questions/97871/8 "2006-07-11T22:47:16Z")

</div>

On 7/11/06, Williams, Ali [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![WilliamsS\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ce7236/32.png) [@WilliamsS\_Ali](https://www.ruby-forum.com/u/WilliamsS_Ali)\
**Post date:** [July 15, 2006, 12:47am UTC](https://www.ruby-forum.com/t/ror-questions/97871/9 "2006-07-15T00:47:49Z")

</div>

> > ((Recognition failed for "/update/24)) if the action itself does exist
> 
> 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. 😛

@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 D. 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 V.
