small medium large xlarge

Generic-user-small
03 Sep 2008, 14:05
Tan YL (19 posts)

Hi,

I have been trying to add the highlight effect to my table but everytime a new record is added the highlighting gives an rjs error. is this because the partial must only have only one <tr></tr> in it and the <table> must be on the other page that is rendering that partial?

View Code:

<% for product in @lineprice %> <% if product.groceries.id == @item.groceries_id %> <% else %> <% end %> <%= product.groceries.brand %> <%= product.groceries.name %> <%= product.quantity %> <%= qx(product,"c") %><% totalC += qx(product,"c")%> <%= qx(product,"n") %><% totalN += qx(product,"n") %> <%= qx(product,"s") %><% totalS += qx(product,"s") %> <%= qx(product,"g") %><% totalG += qx(product,"g") %> <%= qx(product,"ca") %><% totalCa += qx(product,"c") %> item:<%= @item.groceries_id%>Product:<%= product.groceries.id %> <%= cheapest=product.groceries.supermarket.attributes.except('id', 'groceries_id','created_at','updated_at').values.min %> <%= Groceries.namecompare2(product) %><% totalCh += cheapest %> <% end %> ------------------------------------------- Form Code: -------------------------------------------- <% form_remote_tag :url => {:controller => "sgpanel", :action => "add_to_cart", :ad => params[:id] , :bd => groceries.id } do %> <%= submit_tag "Add to Cart" %> <% end %> --------------------------------------------- Controller Code: def add_to_cart @sguser = Sguser.find(params[:ad]) @lineprice = SglineItem.find(:all, :conditions => "sguser_id = #{params[:ad]}") if SglineItem.find(:first, :conditions => ['sguser_id =? and groceries_id =?', params[:ad], params[:bd]])== nil @item = SglineItem.new @item.sguser_id= params[:ad] @item.groceries_id=params[:bd] @item.save! else @item = SglineItem.find(:first, :conditions => ['sguser_id =? and groceries_id =?', params[:ad], params[:bd]]) @item.quantity +=1 @item.save! end respond_to do |format| format.js end end ------------------------------------------------- Js.rjs code: page.replace_html 'calculated', :partial => 'add_to_cart' page[:highlighted].visual_effect :highlight,:startcolor => "#88ff88" ,:endcolor => "#FFFFFF"
Samr_small_pragsmall
03 Sep 2008, 15:14
Sam Ruby (634 posts)

Question: in general, Rails combines layouts, templates, and partials into a single stream and sends the result to the client. RJS produces JavaScript, and that too is sent to the client, with the client, and such scripts operate on the document as the client sees it. As the client as no way of knowing where a partial begins and ends, how you divide up your HTML is up to you.

A suggestion as to something to try: view source in the browser after you get the RJS error. Can you find the tr with an id of highlighted?

Generic-user-small
03 Sep 2008, 15:40
Tan YL (19 posts)

no i can’t. it doesn’t seem to see the id of highlighted.

I know <% if product.groceries.id == @item.groceries_id %> is producing the correct code cause i am debugging now and just using


def add_to_cart @sguser = Sguser.find(params[:ad]) @lineprice = SglineItem.find(:all, :conditions => “sguser_id =#{params[:ad]}”)

	@item = SglineItem.new
	@item.sguser_id= params[:ad]
	@item.groceries_id=params[:bd]
	@item.save!

	respond_to do |format|
	format.js 
	end
	
end

the old product that is added flashes while the new one just stays the same. <tr id=highlighted> does not exist for both the old and the new product which i find is quite odd. what on earth is wrong am i suppose to see <tr id=highlighted> on the old product ?(firing up depot to check it out)

Generic-user-small
03 Sep 2008, 15:45
Tan YL (19 posts)

viewing source doesn’t seem to work as the source displayed on IE or FF is lagging and belongs to the original page not the newly updated ajax portions of the code

Generic-user-small
03 Sep 2008, 16:32
Tan YL (19 posts)

it seems that adding a brand new item will lag only showing the new item in the next click.

meaning that

@item = SglineItem.new @item.sguser_id= params[:ad] @item.groceries_id=params[:bd] @item.save!

is not being saved into the database fast enough for the ajax to see it but why?? In fact i haven’t realised it but the update quantity code probably is lagging behind one click as well. gosh what do i have to do to make saving into mysql directly faster ?? or is there no way ?

Samr_small_pragsmall
03 Sep 2008, 18:23
Sam Ruby (634 posts)

Good analysis, but wrong conclusion. Permit me to explain.

The data is being saved to the database fast enough. In fact it is saved right when you ask it too. But if you look at your controller, the next thing you do is respond with javascript. That javascript will tell the client how to highlight a field that has a certain id attribute on it, but at no point in this processing does the server actually return an updated page to the client with the new attribute or line in it.

This can be done in a number of ways. Each require manipulating the DOM. The server can send down a partial, if there is JavaScript code on the client that “catches” this and knows where to insert it. Or it can send down Javascript instructions which causes either the attribute itself or even an entire row to be inserted into the DOM.

But at this point, we are going beyond Rails. Rails, of course, will be happy to deliver the partial or the JavaScript, but learning more about Prototype and script.aculo.us may be helpful before tackling this problem. I’m not an expert on those technologies myself - I learned how to do these tasks with raw JavaScript and get by with that knowledge – an approach I wouldn’t recommend to anyone these days.

Generic-user-small
04 Sep 2008, 01:47
Tan YL (19 posts)

“Or it can send down Javascript instructions which causes either the attribute itself or even an entire row to be inserted into the DOM.”

wait how come in your example its possible to add a new line to the table but in mine i can’t pass a new row in? aren’t we doing essentially the same taking replacing the page with a totally new page and allowing the for loop to get all the objects inside?

Samr_small_pragsmall
04 Sep 2008, 01:48
Sam Ruby (634 posts)

You certainly can replace the page in JS.

Perhaps it is the formatting, and perhaps you haven’t provided enough information.

Your rjs file has two lines. The first refers to an id named ‘calculated’ and a partial named ‘add_to_cart’. I can find neither in the text you have provided. Take a look at your HTML, and make sure that the element with the id named ‘calculated’ includes the portions of the table you want replaced, and that the partial named ‘add_to_cart’ contains a full and suitable replacement.

Once that is in place, the second line should have no problem.

Generic-user-small
04 Sep 2008, 08:07
Tan YL (19 posts)

sgpanel.html.erb:

< ul class=”panes” id=”panecontrol1”> < li id=”vendor_pane” class=”pane-selected”>

< div id=”calculator”> <%= render :partial => ‘calculator’, :id=> @sguser.id, :object => {@item,@lineprice}%> < /div>

< /li> < li id=”part_pane” class=”pane-unselected”>

< /li> < li id=”map_pane” class=”pane-unselected”>

< /li> < li id=”notes_pane” class=”pane-unselected”>

< /li> < ul>

_add_to_cart.html.erb:

<% for product in @lineprice %> <% if product.groceries.id == @item.groceries_id %> <% else %> <% end %> <%= product.groceries.brand %> <%= product.groceries.name %> <%= product.quantity %> <%= qx(product,"c") %><% totalC += qx(product,"c")%> <%= qx(product,"n") %><% totalN += qx(product,"n") %> <%= qx(product,"s") %><% totalS += qx(product,"s") %> <%= qx(product,"g") %><% totalG += qx(product,"g") %> <%= qx(product,"ca") %><% totalCa += qx(product,"c") %> item:<%= @item.groceries_id%>Product:<%= product.groceries.id %> <%= cheapest=product.groceries.supermarket.attributes.except('id', 'groceries_id','created_at','updated_at').values.min %> <%= Groceries.namecompare2(product) %><% totalCh += cheapest %> <% end %> ------------------------------------------- add_to_cart contains the full replacement but the weird thing is that i need to refresh the whole page to see the new product. It just doesn't make sense. I am nearly doing the same things that u are doing in depot except using mysql instead of a session to store data
Generic-user-small
04 Sep 2008, 08:16
Tan YL (19 posts)

lol i solved it u wouldn’t believe how stupid the answer is. i have to make a 2nd @lineprice2 = SglineItem.find(:all, :conditions => “sguser_id =#{params[:ad]}”) after the if loop because the @lineprice is still referencing the old object which had the old list. i knew it wasn’t anything to do with the rjs. thanks for your help sam

You must be logged in to comment