views:

35

answers:

1

Alright all you wonderful people out there; In my user interface I am building a form where users can add new records via Ajax.

The users will be able add multiple records from the form (it will be cleared after each post) and I am wondering what conventionally would be the best way to setup the form.

If I want to use form_for helpers I need to have an instance of the model to work from i.e.

def index
  @record = Record.new
end

However I am unsure of whether or not this is best from an ajax perspective.

Should I not build an object and just use form_tag and write a method to create the record from my custom form. i.e.

  <%= form_tag "/create_record" do %>
     <%= text_field_tag :record_name %>
     <%= text_area_tag :record_description %>
     <%= submit_tag %>
   <% end -%>

Then grab the attributes in the controller and build my Record manually.

The second way will work, but I don't know if it is best. Can anyone shed light on how the build process works? Do you have to build a new object for each record you want to submit?

Thanks!

A: 

You can use

form_remote_tag

instead of form_tag http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001648

ThinkBohemian
Thanks snow, I am not using prototype for the ajax. (I prefer jQuery), I was more curious as to whether an object that you build has an ID. I will experiment a bit, but thanks for your feedback.
Dustin M.
You should be able to use that with jQuery...i do it with jrails in my projects http://github.com/aaronchi/jrails. I don't think that the ruby object that gets built from the form has an ID parameter, but if you use the create method in your controller, it will generate an ID for you.
ThinkBohemian