Forum: Ruby on Rails troubleshooting an observe_field

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Steve O. (Guest)
on 2006-02-09 14:44
(Received via mailing list)
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:

<h1><label for="searchtext">Live Search:</label></h1>
    <%= 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') %>
    <div id="search_hits"></div>

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
Mark Reginald J. (Guest)
on 2006-02-09 16:13
(Received via mailing list)
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.
Steve O. (Guest)
on 2006-02-09 16:46
(Received via mailing list)
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:

<script type="text/javascript">
//<![CDATA[
new Form.Element.Observer('searchtext', 0.5, function(element, value)
{new Ajax.Updater('search_hits', '/landing/live_search',
{asynchronous:true, evalScripts:true,
onLoaded:function(request){Element.hide('search-indicator')},
onLoading:function(request){Element.show('search-indicator')},
parameters:'q=' + escape($F('searchtext'))})})
//]]>
</script>



Does not work:

<script type="text/javascript">
//<![CDATA[
new Form.Element.Observer('searchtext', 0.5, function(element, value)
{new Ajax.Updater('search_hits', '/portfolio/live_search',
{asynchronous:true, evalScripts:true,
onLoaded:function(request){Element.hide('search-indicator')},
onLoading:function(request){Element.show('search-indicator')},
parameters:'q=' + escape($F('searchtext'))})})
//]]>


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.

Steve
http://www.smarkets.net

</script>
Mark Reginald J. (Guest)
on 2006-02-10 12:13
(Received via mailing list)
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. (Guest)
on 2006-02-10 19:59
(Received via mailing list)
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"}
  Product Load (0.250000)   SELECT 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 20
  Product Columns (0.480000)   SHOW FIELDS FROM
products
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"}
  User Load (0.040000)   SELECT * FROM users WHERE
(users.`login` = 'live_search' ) LIMIT 1
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
Mark Reginald J. (Guest)
on 2006-02-12 02:58
(Received via mailing list)
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.
Steve O. (Guest)
on 2006-02-12 19:38
(Received via mailing list)
Thanks very much, Mark - it was a big help.

Steve
This topic is locked and can not be replied to.