Topic: Generating n rows within a single form

Hi,

I'm currently fleshing out a design that will allow the user to enter 1...n rows of information (each row will have a fixed amount of columns), within a single form, and then submit those rows to a single table.

One thought I'd had was to place a button at the end of each row that will generate the next row for the use to interact with.

Can anyone help me with this?

Regards,

Steve.

Re: Generating n rows within a single form

So what exactly is your question? smile

I had to do something very similar but with one column. It works pretty good with Ajax, while the rows above the new one are rendered as partial and filled out.

Re: Generating n rows within a single form

carnz wrote:

So what exactly is your question? smile

I had to do something very similar but with one column. It works pretty good with Ajax, while the rows above the new one are rendered as partial and filled out.

Question 1: How can I allow the user to enter 1...n rows from within a single form.

Question 2: How can I populate a single table with those rows that were entered within the single form.

Hopefully that is clear :=) If not let me know!

Re: Generating n rows within a single form

I'm gonna try to post a detailed answer later this day.

Until then:
http://courses.voneicken.com/ucsb-cs290 … s_in_Rails
http://www.google.de/search?hl=de&c … &meta=

Re: Generating n rows within a single form

Hi,

Thanks for the links.

From the Google link, I followed this http://www.onlamp.com/pub/a/onlamp/2005 … tml?page=2

and the following code looks like a really good starting point - which is using the form_remote_tag

<html>
  <head>
    <title>Ajax List Demo</title>
    <%= javascript_include_tag "prototype" %>
  </head>
  <body>
    <h3>Add to list using Ajax</h3>
    <%= form_remote_tag(:update => "my_list",
                       :url => { :action => :add_item },
                       :position => "top" ) %>
      New item text:
      <%= text_field_tag :newitem %>
      <%= submit_tag "Add item with Ajax" %>
    <%= end_form_tag %>
    <ul id="my_list">
      <li>Original item... please add more!</li>
    </ul>
  </body>
</html>

Looking at the above code and the screen shots - I can see how I could hard code this to include the exact number of fields (column) before submitting the form.

1 question about the code, the :url command - will that perform the add_item action immediately within the controller, i.e. it will update the d/b when the user clicks on the submit button?

One thing I don't like about the above code is that the display portion is part of the code, could I move this portion out to a partial perhaps and render the display that way?

Re: Generating n rows within a single form

devonps wrote:

1 question about the code, the :url command - will that perform the add_item action immediately within the controller, i.e. it will update the d/b when the user clicks on the submit button?

Of course. If you add that functionality to your action. Actions invoked through any remote method behave the same as normal actions. You can pass params if you like.
The Action for your example could look something like this:

def add_item
  new_item = Item.new
  new_item.name = params[:newitem].to_s
  new_item.save
end

devonps wrote:

One thing I don't like about the above code is that the display portion is part of the code, could I move this portion out to a partial perhaps and render the display that way?

Yes
The list in your example:

<ul id="my_list">
  <%= render :partial => "my_list", :locals => {:items => @items} %>
</ul>

Partial:
<% items.each do |item| %>
  <%= content_tag :li, item.name %>
<% end %>

Your new action:
def add_item
  # save new item
  new_item = Item.new
  new_item.name = params[:newitem].to_s
  new_item.save

  # get all items
  all_items = Item.find(:all)
  # render new list
  render :partial => "my_list", locals => {:items => :all_items}
end


This will be rendered inside the <ul> tags, because of ":update => 'my_list'", which is the id of the list in your example.

Re: Generating n rows within a single form

Thanks very much for the help.

Steve.