# How to do Interactive Page Updates

**URL:** <https://www.ruby-forum.com/t/how-to-do-interactive-page-updates/118043>\
**Category:** Rails\
**Created:** [October 23, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/how-to-do-interactive-page-updates/118043 "2007-10-23T15:16:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@DavidH](https://www.ruby-forum.com/u/DavidH)\
**Post date:** [October 23, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/how-to-do-interactive-page-updates/118043/1 "2007-10-23T15:16:07Z")

</div>

Hi

I think I’m still in the newbie class of Rails programmers and I may  
be trying to run before I can walk so this question may have a very  
simple answer.

I have a page with a small form on it, say Orders. That works fine.

Associated with this form is an additional data set that has a many to  
one relationship with Orders, say Line Items. This too is fine.

What I want to do is display the orders form and below that allow  
users to display, add, edit and delete line items. The line items are  
to be hidden when the form is first displayed with a link that rolls  
down the line items and another that rolls them back up again. When  
they roll down there’s a link next to each allowing the user to delete  
the line item.

Most of this I have working!

They can click a Show Line Items lino\_to\_remote that displays the line  
items:

\<%= link\_to\_remote ‘Show Line Items’,  
:update =\> ‘line\_items’,  
:url =\> { :action =\> ‘list’, :controller =\> ‘line\_items’, :id =\>  
@order.id },  
:complete =\> visual\_effect(:blind\_down, ‘line\_items’)  
%\>

My list method looks like:

def list  
@lineitems = Lineitem.find(:all, :conditions =\> “order\_id =  
#{params[:id]}”)  
render :partial =\> ‘list’  
end

My partial looks like:

Click a line item to edit it.

\<% for lineitem in @lineitems %\> \<% @lineitem = lineitem %\> 

\<%= in\_place\_editor\_field :lineitem, :name, {}, {} %\> \<%= link\_to 'Delete', { :action =\> 'destroy', :id =\> @lineitem, :controller =\> 'lineitem' }, :confirm =\> "Are you sure you want to delete this challenge?", :method =\> :post %\>

\<% end %\>

I have a similar link\_to\_remote that hides them again with a blind\_up.  
I’d like to know if I can do the hide without a call back to the list  
action, I guess I can use any dummy action that avoids a trip to the  
database? Anyway that bit works, I can show and hide my line items  
fine.

What I can’t get my head around is how to refresh my list of line  
items when I add one or delete one? The actual addition and deletion  
work in that the database is updated; but I then want to refresh my  
list and I can’t figure out just how to do this.

Any pointers gratefully received.

Thanks

David

---

<div class="post-metadata">

**Author:** ![DavidH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@DavidH](https://www.ruby-forum.com/u/DavidH)\
**Post date:** [October 23, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/how-to-do-interactive-page-updates/118043/2 "2007-10-23T15:29:00Z")

</div>

Hi David

You may want to look at Active Scaffold…

---

<div class="post-metadata">

**Author:** ![DavidH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@DavidH](https://www.ruby-forum.com/u/DavidH)\
**Post date:** [October 23, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/how-to-do-interactive-page-updates/118043/3 "2007-10-23T20:24:05Z")

</div>

Thanks for the suggestion.

I took a look at Active Scaffold and it looks very comprehensive. It  
will do the functionality I want; but there’s two reasons I don’t want  
to use it:

1. I have all the controller methods and most of the display sorted;  
it’s just removing elements from the list on delete and adding new  
ones on create that I need to do.

2. If I rewrite the whole thing using Active Scaffold to handle all  
the display and control then I’ll have learned nothing about the Rails  
core; which is really what I’m trying to do at the moment. Active  
Scaffold is just another (sophisticated) scaffold and I want to learn  
is how to do it in Rails.

What I think I want is an RJS script to do an element remove if a line  
item has been deleted and an element add if one’s been created. What I  
haven’t been able to figure out is the relationship between the RJS  
files and the rest of the stuff in the view.

Or possible to refresh the list using a replace\_html to update the  
line\_items.

Thanks

David

---

<div class="post-metadata">

**Author:** ![DavidH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@DavidH](https://www.ruby-forum.com/u/DavidH)\
**Post date:** [October 24, 2007, 10:38am UTC](https://www.ruby-forum.com/t/how-to-do-interactive-page-updates/118043/4 "2007-10-24T10:38:12Z")

</div>

DavidH wrote:

> Hi
> 
> I think I’m still in the newbie class of Rails programmers and I may  
> be trying to run before I can walk so this question may have a very  
> simple answer.
> 
> I have a page with a small form on it, say Orders. That works fine.
> 
> Associated with this form is an additional data set that has a many to  
> one relationship with Orders, say Line Items. This too is fine.
> 
> What I want to do is display the orders form and below that allow  
> users to display, add, edit and delete line items. The line items are  
> to be hidden when the form is first displayed with a link that rolls  
> down the line items and another that rolls them back up again. When  
> they roll down there’s a link next to each allowing the user to delete  
> the line item.
> 
> Most of this I have working!
> 
> They can click a Show Line Items lino\_to\_remote that displays the line  
> items:
> 
> \<%= link\_to\_remote ‘Show Line Items’,  
> :update =\> ‘line\_items’,  
> :url =\> { :action =\> ‘list’, :controller =\> ‘line\_items’, :id =\>  
> @order.id },  
> :complete =\> visual\_effect(:blind\_down, ‘line\_items’)  
> %\>
> 
> My list method looks like:
> 
> def list  
> @lineitems = Lineitem.find(:all, :conditions =\> “order\_id =  
> #{params[:id]}”)  
> render :partial =\> ‘list’  
> end
> 
> My partial looks like:
> 
> Click a line item to edit it.
> 
> \<% for lineitem in @lineitems %\> \<% @lineitem = lineitem %\> 
> 
> \<%= in\_place\_editor\_field :lineitem, :name, {}, {} %\> \<%= link\_to 'Delete', { :action =\> 'destroy', :id =\> @lineitem, :controller =\> 'lineitem' }, :confirm =\> "Are you sure you want to delete this challenge?", :method =\> :post %\>
> 
> \<% end %\>
> 
> I have a similar link\_to\_remote that hides them again with a blind\_up.  
> I’d like to know if I can do the hide without a call back to the list  
> action, I guess I can use any dummy action that avoids a trip to the  
> database? Anyway that bit works, I can show and hide my line items  
> fine.
> 
> What I can’t get my head around is how to refresh my list of line  
> items when I add one or delete one? The actual addition and deletion  
> work in that the database is updated; but I then want to refresh my  
> list and I can’t figure out just how to do this.
> 
> Any pointers gratefully received.
> 
> Thanks
> 
> David  
> Hi,

You have to refresh the list when you add or delete an item rt?  
If so  
Store your order\_id in a session variable like session[:id] =  
#{params[:id]} in def list

and then in def destroy add these lines  
@lineitems = Lineitem.find(:all, :conditions =\> “order\_id =  
session[:id]”)  
render :partial =\> ‘list’

Also add :update to link\_to ‘Delete’ as  
\<%= link\_to ‘Delete’, { :action =\> ‘destroy’, :id =\>  
@lineitem, :controller =\> ‘lineitem’ },:update =\> “lineitem”  
:confirm =\> “Are you sure you want to delete this challenge?”,  
:method =\> :post %\>

Hope it helps

Thanks

---

<div class="post-metadata">

**Author:** ![DavidH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@DavidH](https://www.ruby-forum.com/u/DavidH)\
**Post date:** [October 23, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/how-to-do-interactive-page-updates/118043/5 "2007-10-23T21:14:31Z")

</div>

On 23 Oct 2007, at 14:15, DavidH wrote:

> I have a similar link\_to\_remote that hides them again with a blind\_up.  
> I’d like to know if I can do the hide without a call back to the list  
> action, I guess I can use any dummy action that avoids a trip to the  
> database? Anyway that bit works, I can show and hide my line items
> 
> fine.

sure:  
link\_to\_function ‘Hide some stuff’ do |page|  
page.visual\_effect :blind\_up, ‘line\_items’  
end

> What I can’t get my head around is how to refresh my list of line  
> items when I add one or delete one? The actual addition and deletion  
> work in that the database is updated; but I then want to refresh my  
> list and I can’t figure out just how to do this.

2 ways: either you make the rjs for the add/delete actions insert or  
remove the appropriate item from the DOM, or you make both of those  
actions render the complete list, and your delete link looks like

link\_to\_remote ‘delete’, :url =\> {:action =\> ‘delete’, :id =\>  
3}, :update =\> 'line items

Fred
