# Why doesn't my "action" take action?

**URL:** <https://www.ruby-forum.com/t/why-doesnt-my-action-take-action/69941>\
**Category:** Rails\
**Created:** [February 18, 2006, 3:58am UTC](https://www.ruby-forum.com/t/why-doesnt-my-action-take-action/69941 "2006-02-18T03:58:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [February 18, 2006, 3:58am UTC](https://www.ruby-forum.com/t/why-doesnt-my-action-take-action/69941/1 "2006-02-18T03:58:56Z")

</div>

I have the following code in my index.rhtml:

  
\<%= render(:partial =\> ‘form’) %\>

…and the following in my \_form.rhtml  
\<%= form\_remote\_tag(:update =\> “images\_div\_main”,  
:url =\> { :action =\>  
‘get\_reordered\_images’}) %\>  
How many images?  
\<%= text\_field\_tag :numreq %\>  
\<%= submit\_tag “Click to get images” %\>  
\<%= end\_form\_tag %\>

…and of course, this im my Mycontroller\_controller.rb:  
def get\_reordered\_images  
@sock\_serv = TCPSocket.new(“192.168.107.7”, 9000)  
xml = “\<?xml version='1.0' ?\>”  
@sock\_serv.send(xml,0)  
@results = @sock\_serv.recv(1000)  
@sock\_serv.close  
render(:layout =\> false)  
end

and I even have this in my get\_reordered\_images.rhtml file:  
\<%= @results %\>

It works great! Gets me what I want, where I want it on the page. But  
what I also want to do, is “execute” this “action” when the page is  
first rendered. I feel like I should just “call” this from the index  
page, perhaps is ruby brackets, but I’ve spent ours and nothing happens  
until I do the form\_remote\_tag click and then the “replacement” partial  
page appears.

What do I need to do to have “get\_reorder\_images” execute on first  
rendering of html.

Eventually, I will put in logic or a new “action” so that some  
parameters are passed with subsequent calls, but this first call has a  
default mode.

Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [February 18, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/why-doesnt-my-action-take-action/69941/2 "2006-02-18T19:51:22Z")

</div>

Okay, I figured this one out. I have to:  
\<%= render(:partial =\> ‘get\_reordered\_images’) %\> in my index.rhtml file

Then create a \_get\_reordered\_images file in my application’s Views file  
in which I only have:  
\<%= render\_component(:action =\> ‘get\_reordered\_images’) %\>

Which calls my “def get\_reordered\_images” action in my controller.rb  
file.

Now I have two really nagging questions:

1. Can someone explain to me why this is “efficient” (to have all of  
these different types of files just passing along a slightly altered  
call…I know there must be an abstract explanation, but my trivial  
application does show it)?

2. How come some posts that can be really complicated receive multiple  
comments almost immediately, but most of my posts asking simple  
questions, most of the time get no responses?

I’m guessing the multi-post responses are a tight group of colleagues  
and my questions are not of interest, but maybe I’m asking them the  
wrong way.

Thoughts? Comments? write to me directly at DTL “at” [Stanfordalumni.org](http://Stanfordalumni.org)  
(I’m afraid I won’t scroll down to find if anyone posted to me).  
David T-L wrote:

> I have the following code in my index.rhtml:
> 
>   
> \<%= render(:partial =\> ‘form’) %\>
> 
> …and the following in my \_form.rhtml  
> \<%= form\_remote\_tag(:update =\> “images\_div\_main”,  
> :url =\> { :action =\>  
> ‘get\_reordered\_images’}) %\>  
> How many images?  
> \<%= text\_field\_tag :numreq %\>  
> \<%= submit\_tag “Click to get images” %\>  
> \<%= end\_form\_tag %\>
> 
> …and of course, this im my Mycontroller\_controller.rb:  
> def get\_reordered\_images  
> @sock\_serv = TCPSocket.new(“192.168.107.7”, 9000)  
> xml = “\<?xml version='1.0' ?\>”  
> @sock\_serv.send(xml,0)  
> @results = @sock\_serv.recv(1000)  
> @sock\_serv.close  
> render(:layout =\> false)  
> end
> 
> and I even have this in my get\_reordered\_images.rhtml file:  
> \<%= @results %\>
> 
> It works great! Gets me what I want, where I want it on the page. But  
> what I also want to do, is “execute” this “action” when the page is  
> first rendered. I feel like I should just “call” this from the index  
> page, perhaps is ruby brackets, but I’ve spent ours and nothing happens  
> until I do the form\_remote\_tag click and then the “replacement” partial  
> page appears.
> 
> What do I need to do to have “get\_reorder\_images” execute on first  
> rendering of html.
> 
> Eventually, I will put in logic or a new “action” so that some  
> parameters are passed with subsequent calls, but this first call has a  
> default mode.
> 
> Thanks,  
> Dave

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [February 18, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/why-doesnt-my-action-take-action/69941/3 "2006-02-18T20:14:30Z")

</div>

I literally lol’d when I had the ephiphany (re my # 2 question below)  
that when someone responds to your post, the post goes back to the top.  
I didn’t know that!! So those posts that show 12 responses in 2 minutes  
really mean that the last response was two minutes ago, not all 12 of  
them. Doh!

David T-L wrote:

> Okay, I figured this one out. I have to:  
> \<%= render(:partial =\> ‘get\_reordered\_images’) %\> in my index.rhtml file
> 
> Then create a \_get\_reordered\_images file in my application’s Views file  
> in which I only have:  
> \<%= render\_component(:action =\> ‘get\_reordered\_images’) %\>
> 
> Which calls my “def get\_reordered\_images” action in my controller.rb  
> file.
> 
> Now I have two really nagging questions:
> 
> 1. Can someone explain to me why this is “efficient” (to have all of  
> these different types of files just passing along a slightly altered  
> call…I know there must be an abstract explanation, but my trivial  
> application does show it)?
> 
> 2. How come some posts that can be really complicated receive multiple  
> comments almost immediately, but most of my posts asking simple  
> questions, most of the time get no responses?
> 
> I’m guessing the multi-post responses are a tight group of colleagues  
> and my questions are not of interest, but maybe I’m asking them the  
> wrong way.
> 
> Thoughts? Comments? write to me directly at DTL “at” [Stanfordalumni.org](http://Stanfordalumni.org)  
> (I’m afraid I won’t scroll down to find if anyone posted to me).  
> David T-L wrote:

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [February 18, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/why-doesnt-my-action-take-action/69941/4 "2006-02-18T21:19:04Z")

</div>

On Feb 18, 2006, at 10:51 AM, David T-L wrote:

> 1. How come some posts that can be really complicated receive  
> multiple  
> comments almost immediately, but most of my posts asking simple  
> questions, most of the time get no responses?

I have no direct knowledge of your past posts, but I know that from my  
perspective when a poster asks a trivial question that can be answered  
via Google and/or a reading of the Rails book, I don’t bother to  
respond.

When a person asks a question that is unique (and perhaps difficult),  
then  
I want to respond and join because it’s breaking new ground.

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [February 18, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/why-doesnt-my-action-take-action/69941/5 "2006-02-18T21:45:39Z")

</div>

Tom,

I appreciate your feedback.

For me, I don’t ask questions until I’ve been spinning for a few hours.

I can tell you that with the structure of Ruby on Rails and Ajax, there  
are assumptions of location going on under the covers that, as a newbie,  
it is hard to know when something isn’t possible versus when I’m  
misunderstanding the difference between a method, a module, a helper,  
and a component.

I sit here with both “Agile Web D. with Rails” and the pickaxe  
“Programming Ruby” and I’m all over Google with searches, especially for  
error messages.

For example, I thought I should initialize a flag ($new\_search) in the  
controller. The flag is used by a action (method? component?) in the  
controller. Now, it appears the flag gets “re-initialized” everytime  
the action is called. Okay. But I don’t want to put this code in my  
index.rhtml file to initialize it (by that I mean give it the value true  
when the page first renders).

So where should I initialize the flag?

I’m sure this is a trivial question, but for a newbie it opens up a  
whole vista of “getting it”.

Thanks,  
Dave

Tom M. wrote:

> On Feb 18, 2006, at 10:51 AM, David T-L wrote:
> 
> > 1. How come some posts that can be really complicated receive  
> > multiple  
> > comments almost immediately, but most of my posts asking simple  
> > questions, most of the time get no responses?
> 
> I have no direct knowledge of your past posts, but I know that from my  
> perspective when a poster asks a trivial question that can be answered  
> via Google and/or a reading of the Rails book, I don’t bother to  
> respond.
> 
> When a person asks a question that is unique (and perhaps difficult),  
> then  
> I want to respond and join because it’s breaking new ground.
> 
> –  
> – Tom M.

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [February 19, 2006, 12:39am UTC](https://www.ruby-forum.com/t/why-doesnt-my-action-take-action/69941/6 "2006-02-19T00:39:48Z")

</div>

Tom M. wrote:

Thanks for directing your comments in a constructive way. I appreciate  
that it would be easier to “walk away”.

> Here’s the rub, and I’m only saying this to be helpful, and I’m  
> certainly only speaking for myself.
> 
> Have you seen any other code, in the Pickaxe or AWDR or Google  
> seaches on  
> Rails subjects, that have variables that begin with a dollar sign? Ruby  
> variables are not prefixed by dollar signs.

I know this isn’t really your main point, but it did make me wonder how  
I got off on my global variable track. I could NOT find ANY code using  
$nnnnn (I was surprised, I would have sworn I saw it!), but I did find  
some examples (Pickaxe pp 15, 17) where $ is used to demonstrate an  
example of a Global variable.

I finally remembered why I started using it: Pickaxe p.331. I was  
having problems figuring out what was controlling what and I needed to  
use a variable in an action and figure out why it was getting changed or  
not set. In my simplistic approach, I figured I’d avoid the “problems”  
of “undefined variable or method”.

> Now, to answer your question: You _cannot_, under any circumstances with  
> Rails, store values that will persist from one request to the next by  
> using in memory variables. Doesn’t matter where they’re set, doesn’t  
> matter what kind they are. The only way to persist values across  
> requests  
> is via session (disregarding roll your own inter request mechanisms).

That would have been good to read (or remember if I had read it). I’m  
coming from a ‘non-session’, non-object enviornment, so I wasn’t  
thinking of my process in that way. Clear to me now!

> As a general style suggestion, I don’t think anyone would give you  
> advice  
> to set any sort of flag in a view. Setting a flag is about controlling  
> the application, and therefore belongs in the controller.

I buy into that, that’s what was puzzling me.

Thanks for helping me understand the best use of this forum.

Dave

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [February 18, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/why-doesnt-my-action-take-action/69941/7 "2006-02-18T23:32:54Z")

</div>

On Feb 18, 2006, at 12:45 PM, David T-L wrote:

> For example, I thought I should initialize a flag ($new\_search) in the  
> controller. The flag is used by a action (method? component?) in the  
> controller. Now, it appears the flag gets “re-initialized” everytime  
> the action is called. Okay. But I don’t want to put this code in my  
> index.rhtml file to initialize it (by that I mean give it the value  
> true  
> when the page first renders).

Here’s the rub, and I’m only saying this to be helpful, and I’m  
certainly  
only speaking for myself.

Have you seen any other code, in the Pickaxe or AWDR or Google  
seaches on  
Rails subjects, that have variables that begin with a dollar sign? Ruby  
variables are not prefixed by dollar signs.

This mailing list is comprised of some of the most helpful and  
knowledgeable people as any I’ve ever subscribed to. That said, a  
mailing  
list is NOT intended as a programming peer that you can just shoot  
questions out to, as though all the members are sitting next to you in a  
small room.

So, aforementioned helpful and knowledgeable people on the list will  
give  
more time and attention to people that have clearly spent _enough_ time  
to get familiar with the environment. Since they did this themselves,  
they  
know where the sticking point are, as nearly each and everyone of them  
started in the same place you did: new to Ruby, new to Rails, coming  
from another language, Pickaxe and AWDR on knee.

Now, to answer your question: You _cannot_, under any circumstances with  
Rails, store values that will persist from one request to the next by  
using in memory variables. Doesn’t matter where they’re set, doesn’t  
matter what kind they are. The only way to persist values across  
requests  
is via session (disregarding roll your own inter request mechanisms).

As a general style suggestion, I don’t think anyone would give you  
advice  
to set any sort of flag in a view. Setting a flag is about controlling  
the application, and therefore belongs in the controller.

–  
– Tom M.
