Topic: Ajax call to update an association freezes the server

Hello folks I had previously posted this in the database section, but I was able to solve the db issue, now this is stricty an Ajax problem.


I am trying to implement an ajax update of a one-to-one association. I have a Course model which has_one :color and a Color model which belongs_to :course. I have a course_id column in the colors table.

I made a custom dropdown (actually, a <table>) to choose a color visually; for this reason I had to implement an ajax update; there might be a smarter way, but I couldn't think of any.

/views/courses/edit.rhtml

    <table>
        <% for color in @colors %>
            <tr class="<%= color.hex %>"><td id="<%= color.id %>" class="<%= color.hex %>" onclick="<%= remote_function(:url => { :action => :update_color, :id => @course.id, :color_id => color.id }) %>"><%= color.hex %></td></tr>
        <% end %>
        </table>

Here is the function code:

/controllers/courses_controller.rb

def update_color
   
    @course = Course.find(params[:id])
    @color = Color.find(params[:color_id])
    @course = @color 
     
    @course.save
   
end

The db part seems to be working; the course_id column of the colors table gets updated. The problem is, the server stops working, it is frozen; I guess it is waiting for a response.
Can you help me in finding the problem? Also, if you can suggest a better implementation instead of remote_function, let me know.
Thanks in advance,
Davide