Topic: rjs works with webrick but not lighttpd

I've got an issue that is vexing me.  My code works when I use it with webrick but not with lighttpd. I have other ajax effects working on the server but this is my only use of RJS.

Here is an example from my rhtml file of calling the rjs (events_by_region is an rjs file):

<%= link_to_remote(image_tag("../images/sale.gif",{:alt => "SALE"}), :url => {:controller => 'events',:action => :events_by_region, :outting_id => '1', :evt_id_list => evt_id_list, :evt_latitude => p, :evt_longitude => q }) %> <%=image_tag("../images/line.gif")%>

And here is the RJS file that is getting called:

page << "var a=0;"
if @mapsnew
    for mapsnew in @mapsnew   
          page << "var e=new GLatLng(#{params[:evt_latitude]}, #{params[:evt_longitude]}).distanceFrom(new GLatLng( #{mapsnew.latitude} , #{mapsnew.longitude.to_f}  ))/1000;"
          page << "if(a==0){"
          @evnt=Event.find(mapsnew.event_id)
          page.replace_html 'maplist', content_tag('li', "<a href='javascript:myclick(#{mapsnew.id})' onmouseover='mymouseover(#{mapsnew.id})' onmouseout ='mymouseout(#{mapsnew.id})'>#{@evnt.event_head[0..25]}...</a>" , :id => 'list_item' )         
          page << "}else{"
          page.insert_html :bottom, 'maplist', content_tag('li', "<a href='javascript:myclick(#{mapsnew.id})' onmouseover='mymouseover(#{mapsnew.id})' onmouseout ='mymouseout(#{mapsnew.id})'>#{@evnt.event_head[0..25]}...</a>" , :id => 'list_item' )         
          page << "}"
          page << "a=a+1;"
    end
else   
    page.replace_html 'maplist', content_tag('li', "No events to list!!!" , :id => 'list_item' )         
end

Like I said.. it works via webrick (tested on my local machine AND on my server) but not when using lighttpd.  If it matters the server is lighttpd 1.4.13 (also tried with 1.4.11) on Ubuntu Dapper Drake.

Thanks in advance..

Re: rjs works with webrick but not lighttpd

Can you provide any details on what exactly isn't working? Does clicking the link send a request? Try going to the RJS action directly by typing the URL in the browser and see if it is returning any JavaScript.

Railscasts - Free Ruby on Rails Screencasts

Re: rjs works with webrick but not lighttpd

I have four icons that each correspond to a category (1 through 3 and 4 being ALL).  Clicking on an icon is supposed to filter the default (ALL) to show only items in the category.  Works via webrick but not via lightty.  (e.g. events are either related to music, food, or speeches. The default shows all but clicking the music icon should only show music events).

I have firebug installed and when I click one of the icons it shows a post but no filtering happens:

http://myapp.net/events/events_by_regio … ;outting_i

The response is even showing up correctly (in the firebug console)
var a=0;
alert('inside events_by_region');
Element.update("maplist", "<li id=\"list_item\">No events to list!!!</li>");

But the page isn't being updated and the alert('inside events_by_region') doesn't come up.

Re: rjs works with webrick but not lighttpd

If the Javascript response is correct then I don't see how changing the webserver could be the problem because at this point the web browser handles everything.

Are you testing both webrick and lighty on the same browser? What about the rails environment, are they both in development/production? Or is one in development and the other in production? RJS behaves a little differently in production (for example, it doesn't wrap everything in a try block AFAIK). Perhaps that is affecting it.

Railscasts - Free Ruby on Rails Screencasts

Re: rjs works with webrick but not lighttpd

Here is a comparison of the firebug headers:

WORKS (webrick)
Cache-Control: no-cache
Connection: close
Date: Tue, 10 Oct 2006 19:56:41 GMT
Content-Type: text/javascript; charset=UTF-8
Server: WEBrick/1.3.1 (Ruby/1.8.4/2005-12-24)
Content-Length: 2014
Set-Cookie: _session_id=5d3950e52b565ec95c26509d573f913f; path=/


DOES NOT WORK (lighttpd)
Connection: close
Transfer-Encoding: chunked
38.909643Content-Type: text/javascript
Set-Cookie: _session_id=246ae40922c0fd45f1514aeb4bf790ae; path=/
Cache-Control: no-cache
Date: Tue, 10 Oct 2006 19:41:35 GMT
Server: lighttpd

no idea why the latitude is showing up right before Content-Type but that's probably the issue.

I tried manually sticking a @headers in the controller but that didn't help.

Update: regarding your questions, yes I am testing on the same browser but have tried it on others with the same result.

The test I did to convince myself it was lightty was shutting down lighttpd and booting webrick on port 80.  Webrick on 80 works but lighttpd on 80 (same server) does not.

Last edited by viniosity (2006-10-10 16:13:44)

Re: rjs works with webrick but not lighttpd

Yeah, I think you found the problem (the numbers before content-type). Are you calling "print" anywhere in the application? Not sure why it would react differently depending upon the server though.

Railscasts - Free Ruby on Rails Screencasts

Re: rjs works with webrick but not lighttpd

I AM calling a print. Ug!  I didn't even think about that. Problem solved!!! Thank you!!!

Re: rjs works with webrick but not lighttpd

I'm having sort of the same problem, except rather than lighty, I am using Apache. I am in the development environment. By "print" do you mean puts statements? I have a few of those mixed around...but I don't see how that would affect anything?

I'm not sure how to get firebug headers to show between the servers. But everything seems to be working fine on the Webrick server, but views with associated rjs files are not working on Apache.

I was searching the net, and people have mentioned that I need to define ETag in the headers. How/where does this need to be done?

Essentially, the only change I am making is switching the server and port. Everything else remains the same...

Re: rjs works with webrick but not lighttpd

Here are the methods I used to solve my issues.  Firebug is your friend in this case:

http://www.urbanpuddle.com/articles/200 … t-lighttpd
http://www.urbanpuddle.com/articles/200 … ighttpd-ii

Re: rjs works with webrick but not lighttpd

Jammer1982 wrote:

By "print" do you mean puts statements? I have a few of those mixed around...but I don't see how that would affect anything?

Yep, either puts or print will likely cause a problem. This is because it outputs the results directly to the browser, before all of the headers have been sent. This can cause certain browsers/servers to not handle the RJS response properly because the headers aren't correct.

Railscasts - Free Ruby on Rails Screencasts

Re: rjs works with webrick but not lighttpd

You shouldn't use "puts" for debugging.  Use logger.debug instead (or logger.info or logger.warn or logger.error).  The output will go directly to the log and won't interfere with the layout of your pages or with the headers that are being sent.

Re: rjs works with webrick but not lighttpd

Thank you viniosity for those links. I have now installed Firebug.

Thank you ryanb for confirming that I shouldn't have puts in my app. Getting rid of the puts solved the problem.

Thank you rafeco for pointing out that I should use logger.debug instead.