# Troubleshooting an observe\_field

**URL:** <https://www.ruby-forum.com/t/troubleshooting-an-observe-field/64990>\
**Category:** Rails\
**Created:** [February 9, 2006, 1:44pm UTC](https://www.ruby-forum.com/t/troubleshooting-an-observe-field/64990 "2006-02-09T13:44:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [February 9, 2006, 1:44pm UTC](https://www.ruby-forum.com/t/troubleshooting-an-observe-field/64990/1 "2006-02-09T13:44:24Z")

</div>

Hi,

I’ve got a live\_search field in a partial that is on several different  
pages. On all pages, except 1, it works fine. On the page it doesn’t  
work  
on, it seems it never executes it action.

Here’s my observe\_field code inside the partial:

# Live Search:
 \<%= text\_field\_tag :searchtext %\> \<%= observe\_field(:searchtext, :frequency =\> 0.5, :update =\> :search\_hits, :url =\> { :action =\> :live\_search }, :loading =\> "Element.show('search-indicator')", :loaded =\> "Element.hide('search-indicator')", :with =\> "'q=' + escape($F('searchtext'))")%\> \<%= image\_tag("indicator.gif", :id =\> 'search-indicator', :style =\> 'display:none') %\> 

Here is my action (inside application.rb)  
def live\_search  
@search = @params[:q]  
@results = Product.sql\_for\_search\_results(@search)  
render(:partial =\> “partials/search\_layout”, :layout =\> false)  
end

If I put a breakpoint in live\_search, it never gets tripped by the  
observe\_field on the one problem page.

The problem page tries to re-render itself inside the update div.

It happens in both firefox and IE6.

I’m not sure how else to troubleshoot this.

Any suggestions?

Thanks,

Steve  
[http://www.smarkets.net](http://www.smarkets.net)

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [February 9, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/troubleshooting-an-observe-field/64990/2 "2006-02-09T15:13:52Z")

</div>

Steve O. wrote:

> ```
> :update => :search_hits,
> 
> ```
> 
> observe\_field on the one problem page.
> 
> The problem page tries to re-render itself inside the update div.

It looks like your :url parameter is actually being set to an empty  
hash, which you can verify by looking at the HTML source and seeing  
that the path in the Ajax call is that of the rendered page itself.

If you have accurately copied you code into your post the  
only thing I can think of is that you are using an earlier version  
of Rails for which hash values for urls must be strings rather than  
symbols. Try :url =\> { :action =\> ‘live\_search’ }

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [February 9, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/troubleshooting-an-observe-field/64990/3 "2006-02-09T15:46:26Z")

</div>

Thanks Mark. I’m using 1.0. I tried changing my :action from  
:live\_search to  
‘live\_search’ to no avail.

Here is what the html source looks like for a page that works and the  
problem page:

Works:

Does not work:

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [February 10, 2006, 11:13am UTC](https://www.ruby-forum.com/t/troubleshooting-an-observe-field/64990/4 "2006-02-10T11:13:48Z")

</div>

Steve O. wrote:

> Thanks Mark. I’m using 1.0. I tried changing my :action from  
> :live\_search to ‘live\_search’ to no avail.
> 
> Just to be clear, my live\_search method is not in my landing controller, but rather in  
> application.rb,  
> so it should be available to both.
> 
> Curious.

Yes. The url in the Ajax call is correct. What do you see in your  
Rails log when the request is sent? Does copying the live\_search  
method into your Portfolio controller make it work?

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [February 12, 2006, 1:58am UTC](https://www.ruby-forum.com/t/troubleshooting-an-observe-field/64990/5 "2006-02-12T01:58:21Z")

</div>

Steve O. wrote:

> Parameters: {“action”=\>“index”, “id”=\>“live\_search”, “q”=\>“pau”,  
> “controller”=\>“portfolio”}  
> …  
> I do have a custom route for this /portfolio/index page:
> 
> map.portfolio ‘portfolio/:id’, :controller =\> ‘portfolio’, :action =\>  
> ‘index’
> 
> I don’t know if that is involved in this.

Yes, the custom route is the problem. It’s taking the “live\_search”  
action as an id. Add an “:id =\> /[0-9]+/” option to the route  
so that only numeric ids are re-routed.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [February 12, 2006, 6:38pm UTC](https://www.ruby-forum.com/t/troubleshooting-an-observe-field/64990/6 "2006-02-12T18:38:14Z")

</div>

Thanks very much, Mark - it was a big help.

Steve

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [February 10, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/troubleshooting-an-observe-field/64990/7 "2006-02-10T18:59:53Z")

</div>

Copying live\_search method into my portfolio controller doesn’t help.

Here are two log examples:

## WORKS: (in landing\_controller)

## Processing LandingController#live\_search (for 127.0.0.1 at 2006-02-10 11:37:29) [POST] Parameters: {“action”=\>“live\_search”, “q”=\>“pau”, “controller”=\>“landing”} e[4;36;1mProduct Load (0.250000)e[0m e[0;1mSELECT p.id, p.product FROM products p WHERE lower(p.prdMFG) LIKE ‘%pau%’ OR lower(p.product) LIKE ‘%pau%’ OR lower(p.asin) LIKE ‘%pau%’ AND p.active = 1 LIMIT 20e[0m e[4;35;1mProduct Columns (0.480000)e[0m e[0mSHOW FIELDS FROM productse[0m Rendered partials/\_search\_results (1.03000)

## FAILS: (in portfolio\_controller)

## Processing PortfolioController#index (for 127.0.0.1 at 2006-02-10 11:39:09) [POST] Parameters: {“action”=\>“index”, “id”=\>“live\_search”, “q”=\>“pau”, “controller”=\>“portfolio”} e[4;35;1mUser Load (0.040000)e[0m e[0mSELECT \* FROM users WHERE (users.`login` = ‘live\_search’ ) LIMIT 1e[0m Attempt to access portfolio without a login id Redirected to [http://127.0.0.1:3000/](http://127.0.0.1:3000/)

It seems like what is happening is it is re-executing the code in my  
index  
method in the portfolio controller. That method looks like this:

def index  
store\_location  
@uid = 0  
@uid = @session[:user].id if user\_logged\_in?  
@tid = User.find\_by\_login(@params[:id])  
@pageTitle = “#{@tid.login}'s Portfolio”  
@cash = Trade.find\_by\_user\_id(@tid.id, :order =\> “updated\_on  
DESC”, :limit =\> 1)  
@list = Trade.myportfolio(@tid.id)  
rescue  
logger.error(“Attempt to access portfolio without a login id”)  
flash[:notice] = ‘Sorry, I could not find that page’  
redirect\_to(home\_url)  
end

It is re-executing the @tid = User.find\_by\_login code. What could cause  
that?

I do have a custom route for this /portfolio/index page:

map.portfolio ‘portfolio/:id’, :controller =\> ‘portfolio’, :action =\>  
‘index’

I don’t know if that is involved in this.

Any suggestions are appreciated.

Thanks,

Steve  
[http://www.smarkets.net](http://www.smarkets.net)
