2

我刚刚为 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

这是视图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 %>

它执行如下 SQL 命令:

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

它会导致 SQL 语法错误。我认为我的 ruby​​ 语法是错误的,那么我该如何解决?

4

1 回答 1

3

从此更改您的 ruby​​ 代码:

"post_id ? AND ip_address = ?"

"post_id = ? AND ip_address = ?"
于 2012-05-23T02:52:26.377 回答