I came up with a solution that works really nicely for this.. The
biggest prob I had implementing it was coming up with unique IDs to
represent ajaxly added sub-items.. it''s a bit wonky but I use the
current time in millis to represent the IDs for newly added items..
When deleting or adding I check if the ID exists and assume if it
doesn''t that it must be one of these crazy time based IDs..
main rhtml:
<h3>Other Physician Notes <%= link_to_remote( image_tag
( ''edit_add.png'' ), :update => ''notes'',
:position
=> :bottom, :complete => visual_effect( :appear, ''notes''
), :url =>
{ :action => ''add_note'' } ) %></h3>
<div id="notes">
<%= render :partial => ''note'', :collection =>
@checkout.notes %>
</div>
in the controller:
def add_note
note = Note.new()
note.id = Time.now.to_i #this won''t actually be used as the id
note.date = Time.now.strftime( DAY_FORMAT )
render :partial => ''note'', :locals => { :note =>
note }
end
def remove_note
note = find_note( @params[:id] )
if ! note.nil?
note.destroy #This is not a new note but an old one..actually
delete it
end
render :text => ''''
end
def find_note( id = 1 )
begin
return Note.find( id )
rescue ActiveRecord::RecordNotFound
return nil
end
end
then _note.rhtml:
<% @note = note
div_name = "note_#{note.id}" %>
<div id="<%= div_name %>">
<p>
<%= check_box ''note[]'', ''checked''
%>
<%= collection_select ''note[]'',
''physician_id'', @physicians,
''id'', ''name'' %>
<%= text_field ''note[]'', ''note''
%>
<%= text_field ''note[]'', ''date''
%>
<%= link_to_remote( image_tag( ''edit_remove.png'' ),
:update =>
div_name, :loading => visual_effect( :blind_up, div_name ), :confirm
=> ''Delete is permanent. Are you sure?'', :url => {
:action =>
''remove_note'', :id => note.id } ) %>
</p>
</div>
On 15-Sep-05, at 1:20 PM, Wilson Bilkovich wrote:
> If you use render(:partial, :collection), you gain access to a counter
> variable with the same name as your partial form.
> For example, I have a form called _member.rhtml
> It''s a complex multiple-entry ajax''ed table thingy..
Here''s a snippet
> from it, that searches for individuals in a big table and does
> different things depending on how many matches there are. I''m
still
> not good at formatting code in Gmail, so forgive me if this is ugly.
>
> <td id="search<%= member_counter %>">
> <%= link_to_remote(image_tag("search_icon.gif"), :loading
=>
> "$(''search_results'').innerHTML =
''''Searching..",
> :complete => "eval(request.responseText)" ,
> :url => { :action => "search_by_ssn" },
> :with =>
"''ssn=''+$(''member_#{member_counter}_ssn'').value
>
+''&''+''member_id=''+#{member_counter}")
> %>
> </td>
>
> The call to the partial just looks like:
> render(:partial => ''member'', :collection =>
@members)
>
> There might be an easier way. If so, hopefully someone will make fun
> of me, and I''ll learn something. Heh.
>
> I know it doesn''t exactly match what you''re trying to do,
but it
> might help.
>
> --Wilson.
>
>
> On 9/14/05, Stephen Caudill
<scaudill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
>> I have a form that allows you to enter an address. I''ve got a
>> link_to_remote callback that allows you to add another set of address
>> form fields...
>>
>> <%= link_to_remote "Add Another Address",
>> :update => "mofields",
>> :url => { :action => "add" },
>> :position => "after" %>
>>
>> controller is dead simple:
>>
>> def add
>> render :partial => ''address_form''
>> end
>>
>> In the UI, all goes as expected, however, the fields receive no index
>> when adding new ones, so the first set of fields with their
>> particular
>> names is the only one that ever gets posted to the database.
>>
>> Which leads me to ask:
>> Is there a built in function to make the added fields indexed? If
>> not
>> how can I achieve a similar effect?
>>
>> Ideally I''d like to have the form post a nested set of
collections
>> that I could loop through and do inserts with.
>>
>> Thanks in Advance,
>> Stephen
>>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>