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
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.
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:
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.
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.
Thanks very much, Mark - it was a big help.
Steve
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/
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