搜索 :all 时出错

Error searching for :all

我目前正在尝试创建一种搜索方法。我有一个数据库全部设置;但是,我 运行 陷入了错误:

ActiveRecord::RecordNotFound in ArticlesController#index

和:

Couldn't find Article with 'id'=all

这里是相关代码:

Articles_controller.rb

class ArticlesController < ApplicationController
  def index
    @articles = Article.all

    @articles = Article.search(params[:id])
  end

  def show
    @article = Article.find(params[:search])
  end

  def new
    @article = Article.new
  end

  def edit
    @article = Article.find(params[:id])
  end

  def create
    @article = Article.new(params.require(:article).permit(:title, :text))

    if @article.save
      redirect_to @article
    else
      render 'new'
    end
  end

  def update
    @article = Article.find(params[:id])

    if @article.update(article_params)
      redirect_to @article
    else
      render 'edit'
    end
  end

  def destroy
    @article = Article.find(params[:id])
    @article.destroy

    redirect_to articles_path
  end

  private
    def article_params
      params.require(:article).permit(:title, :text)
    end
end

article.rb

class Article < ActiveRecord::Base

  validates :title, presence: true,
                    length: { minimum: 5 }

    def self.search(search)
    if search
        @article = Article.find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
    else
        @article = Article.find(:all)
    end
    end

end

index.rb

<h1>Listing articles</h1>

<%= link_to 'New article', new_article_path %> 

<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th colspan="3"></th>
  </tr>

  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
      <td><%= link_to 'Edit', edit_article_path(article) %></td>
      <td><%= link_to 'Destroy', article_path(article),
              method: :delete,
              data: { confirm: 'Are you sure?' } %></td>
    </tr>
  <% end %>


  <%= form_tag articles_path, :method => 'get' do %>
     <p>
        <%= text_field_tag :search, params[:search] %>
        <%= submit_tag "Search", :name => nil %>
     </p>
 <% end %>

</table>

抱歉让您浏览所有代码。我收到的错误来自 运行 localhost:3000/articles,我从服务器收到这些错误消息。我应该指出,我对 Rails 上的 Ruby 和 Ruby 还是很陌生;但是,我的目标是学习并发现看到正确的代码对我有很大帮助(我有阅读障碍并且倾向于视觉学习)。

非常感谢您的帮助,在此先致谢。

为什么索引方法中有@articles = Article.search(params[:id])

此外,堆栈跟踪会告诉您错误发生在哪一行

我觉得find不能带:all。 documentation 表示

"使用 find 方法,您可以检索与指定的 主键 对应的对象,该对象匹配任何提供的选项。我认为这就足够了

Article.where('name LIKE ?', "%#{search}%")

或者如果您找到所有文章

Article.all