Skip to main content
2 of 2
Removed off-topic request from question
200_success
  • 144.1k
  • 22
  • 188
  • 473

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 => @ip, :konkurrencer_id => 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 => 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?