Topic: Using Ajax to Add an Artist to an Album

I have a New Album Page.
The album's artist can be selected from a drop down box.

If the artist is not in the database i want to be able to add one via ajax and have the drop down box update.
I'm not sure how to do it. I have tried to use remote_form_tag and link_to_remote but I haven't got it yet.

Merry Christmas.

Re: Using Ajax to Add an Artist to an Album


The easiest way to do it would be with form_remote_tag.

You could hide the form in a div like so:

<div id="form_to_add_artist style="display:none">
  <%= form_remote_tag(:loading => "'loader'); Element.hide('form_to_add_artist');",
        :complete => "Element.hide('loader');'form_to_add_artist');",
        :url => {:action => :add_artist}) %>
  <label for="artist_name">Name:</label> <%= text_field "artist", "name" %><br />
    <%= submit_tag value = "Add Artist" %>
  <%= end_form_tag %>

in the controller you would have:
def add_artist
  @artist = Artist.create(params[:artist]

hope that helps


Rails Development - Agile rails consulting and development for startups on a budget
Flvorful Blog - Some ramblings
Flvorful's Open Source Projects - Some handy hacks for your rails projects.