2

I just wrote controller for ajax call like this:

def create 
    @like = Like.new(params[:like])
    post = params[:like][:post_id]
    uid = params[:like][:ip_address]
    @extant = Like.find(:last, :conditions => ["post_id ? AND ip_address = ?", post, uid])
    last_like_time = @extant.created_at unless @extant.blank?
    curr_time = Time.now

    if ((@extant && curr_time - last_like_time >= 24.hours) || @extant.blank?)
      respond_to do |format|
        if @like.save
          format.js
          format.html { redirect_to :back }
        else
          format.html { redirect_to posts_path }
          format.json { render :json => @like.errors, :status => @unprocessable_entity }
        end
    end

    else
      render :js => "alert('You already liked this.');"
    end

end

And this is view erb.

<%= form_for(@like, :remote => true) do |f| %>
<%= f.hidden_field "post_id", :value => @post.id %>
<%= f.hidden_field "ip_address", :value => request.remote_ip %>
<%= submit_tag "Like" %>
<% end %>

And it executes SQL command like this:

SELECT  "likes".* FROM "likes"  WHERE (post_id '1' AND ip_address = '127.0.0.1') ORDER BY "likes"."id" DESC LIMIT 1

It causes SQL syntax error. I think my ruby syntax is wrong, so how can I fix?

1 Answer 1

3

Change your ruby code from this:

"post_id ? AND ip_address = ?"

to

"post_id = ? AND ip_address = ?"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.