Using link_to_remote to add a record

-- -- wrote:

I am trying to use link_to_remote to simply add a record to a database table called "ratings". I've seen some examples of this being done but can't seem to get it to work. Any ideas about what I'm doing wrong?

What's your functional test case look like?

Here is what I have in my view:

<%= link_to_remote( "1", {:url => { :controller => "ratings", :action => "rate", :rateable_id => "1", :rating => "1", :rateable_type => "article"}}) %>

Here is what I have in RatingsController:

def rate    @rating = Rating.new(:rateable_id => params[:rateable_id],                          :rating => params[:rating],                          :rateable_type => params[:rateable_type]) end

I found that I can successfully write to the database through the console with:

rating = Rating.new rating.rating = 3 rating.save

Why don't you have a save! in the controller? and why you didn't use .create! ?

At a guess, your test case should look a little like this:

    fixtures :ratings # this automatically zilches the last test's records

  def test_new_rate     xhr :get, :rate, :rateable_id => 42, :rating => 1.1,                           :rateable_type => 'hot dogs'

    rate = Rating.find_by_rateable_id 42     assert_equal 'hot dogs', rate.rateable_type   end

To put it another way, all the experiments you currently do with the console, you should instead do as test cases. Then they help you avoid bugs and develop faster.