How to do Interactive Page Updates

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 David

You may want to look at Active Scaffold…

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

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

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