Dustin M.
Dustin M.

Reputation: 2974

Building and submitting an Active Record form by Ajax in Rails best practices?

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!

Upvotes: 2

Views: 422

Answers (1)

Schneems
Schneems

Reputation: 15828

You can use

form_remote_tag

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

Upvotes: 1

Related Questions