ROR questions

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

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

Ta,
Chuck V.

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

Ta,
Chuck V.


Rails mailing list
[email protected]
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

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

shai <[email protected]…> 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

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! :wink:

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">*&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…

On 7/11/06, Isak H. [email protected] wrote:

browser.

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

Isak

On 7/11/06, Williams, Ali [email protected] 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

((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. :stuck_out_tongue:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs