在ArticlesController中的ActiveRecord :: PreparedStatementInvalid

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在ArticlesController中的ActiveRecord :: PreparedStatementInvalid相关的知识,希望对你有一定的参考价值。

我正在尝试使用Ruby on Rails创建一个迷你博客。当我想在articles_controller.rb上添加搜索操作时遇到此错误。搜索表单位于导航栏。如果搜索表单为空,则没有错误。如果我在搜索表单中输入了某些内容,则会出现此错误wrong number of bind variables (2 for 1) in: text LIKE ? or title LIKE

错误日志

开始获取“/ articles / search?utf8 =%E2%9C%93&search = kawaii&commit = Search”for 127.0.0.1 at 2019-02-21 13:21:02 +0900(0.4ms)SELECT“schema_migrations”。“version” FROM“schema_migrations”ORDER BY“schema_migrations”。“version”ASC↳/Users/igarashinobuo/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record /log_subscriber.rb:98由ArticlesController处理#Search as html参数:“utf8”=>“✓”,“search”=>“kawaii”,“commit”=>“搜索”在14ms内完成500内部服务器错误(ActiveRecord:0.0ms)

ActiveRecord :: PreparedStatementInvalid(错误的绑定变量数(2个为1):text LIKE?或title LIKE):

app / models / article.rb:8:在search' app/controllers/articles_controller.rb:23:insearch'

控制器/ articles_controller.rb


def search
    if params[:search].blank?
      @articles = Article.all
    else
      @articles = Article.search(params)
    end
  end

车型/ article.rb


  def self.search(params)
      articles = Article.where("text LIKE ? or title LIKE", "%# 
      params[:search]%", "%#params[:search]%") if params[:search].present?
      articles
  end
end

意见/文章/ search.html.erb


<div class="row">
  <div class="col"></div>
  <div class="col-md-10">
    <h1>
      記事リスト</h1>
    <p>
      <%= link_to '新規記事作成', new_article_path, class:'btn btn-lg btn-info' %>
    </p>

    <table class="table table-hover table-striped table-responsive-xs table-info">
      <thead>
        <tr>
          <th>Title</th>
          <th>Text</th>
          <th colspan="3">Editing Options</th>
        </tr>
      </thead>
      <tbody>
        <% @articles.each do |article| %>
          <tr>
            <td>
              <%= article.title %>
            </td>
            <td>
              <%= truncate(article.text, length: 75) %>
            </td>
            <td>
              <%= link_to 'SHOW', article_path(article), class: 'btn btn-sm btn-info' %>
            </td>
            <td>
              <%= link_to 'Edit', edit_article_path(article), class: 'btn btn-sm btn-warning' %>
            </td>
            <td>
              <%= link_to 'Delete',article_path(article), class: 'btn btn-sm btn-danger', method: :Delete, data: confirm: '本当に削除しますか ?' %>
            </td>
          </tr>
        <% end %>
      </tbody>
    </table>
    <p>
      <%= link_to 'Home', welcome_index_path, class: 'btn btn-sm btn-info' %>
    </p>
  </div>
  <div class="col"></div>
</div>


答案

我想你错过了?对于查询的第二个参数:

  def self.search(params)
      articles = Article.where("text LIKE ? or title LIKE ?", "%# 
      params[:search]%", "%#params[:search]%") if params[:search].present?
      articles
  end

以上是关于在ArticlesController中的ActiveRecord :: PreparedStatementInvalid的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 5 中间件“所有者”?

java 动作栏后退按钮。字体:https://stackoverflow.com/questions/10108774/how-to-implement-the-android-actionbar-

Cakephp 3 - 使用_joinData属于ToMany

React 中的 Redux 详解:

ActionBar 中的选项卡数

切换 JPanel