Rails 3.2 ActiveRecord SQL语法错误

时间:2021-07-17 09:50:25

I just wrote controller for ajax call like this:

我刚为这样的ajax调用编写了控制器:

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:

它执行这样的SQL命令:

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?

它会导致SQL语法错误。我认为我的ruby语法错了,所以我该如何解决?

1 个解决方案

#1


3  

Change your ruby code from this:

从这个更改您的ruby代码:

"post_id ? AND ip_address = ?"

to

"post_id = ? AND ip_address = ?"

#1


3  

Change your ruby code from this:

从这个更改您的ruby代码:

"post_id ? AND ip_address = ?"

to

"post_id = ? AND ip_address = ?"