Rails controller method to accept one rating per IP address via AJAX My rating controller:

  def rate
    @ip = request.remote_ip
    @konkurrencer = Rating.where(:IP =&gt; @ip, :konkurrencer_id =&gt; params[:id])
    if @konkurrencer.empty?
      params[:kon][:IP] = request.remote_ip
      params[:kon][:konkurrencer_id] = params[:id]
      @konkurrencer = Rating.new(params[:kon])
      @konkurrencer.save
      @konkurrencer.konkurrencer.rating_score += params[:kon][:ratings].to_i
      @konkurrencer.konkurrencer.ratings += 1
      @konkurrencer.save
    else
      // render error message
    end
    render :nothing =&gt; true
   end
end


My AJAX:

 jQuery.ajax({
     url: frm.attr('action'), //your server side script
     data: frm.serialize(), //our data
     type: 'POST',
     success: function (data) {
          //write here something when everything went ok

     },
     error: function (jxhr, msg, err) {
         alert(msg); //something went wrong.
     }
 });


Have do I make my controller more DRY?
Controller code looks good.. You only need to refactor code little bit so that it will look more clean &amp; robust. otherwise rest things are ok. regarding ajax response, you need to implement success/failure response either inside controller or js file.

just to give idea on implementing ajax response inside controller

def update
  render :update do |page|
    if success
      # here replace_html is prototype helper method
      page.replace_html "DIV_ID", "PARTIAL"
      # if you are using jquery then you can execute js code like
      page &lt;&lt; "js code goes here"
    else
      page.replace_html "ERR_DIV", "Error occured"
    end
  end
end