【问题标题】:Error searching for :all搜索时出错:全部
【发布时间】:2016-06-29 10:31:17
【问题描述】:

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

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,我从服务器收到这些错误消息。我应该注意,我对 Ruby 和 Ruby on Rails 还是很陌生。但是,我的目标是学习并发现看到正确的代码对我有很大帮助(我有阅读障碍,并且倾向于成为视觉学习者)。

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

【问题讨论】:

  • @article = Article.find(:all) 模型中这是什么?

标签: ruby-on-rails ruby variables search textbox


【解决方案1】:

我认为 find 不能取 :all。 documentation

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

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

或者如果你找到所有的文章

Article.all

【讨论】:

  • 另一个问题,在我的搜索过程中,后端看起来像这样Started GET "/articles?search=asd&amp;utf8=%E2%9C%93" for ::1 at 2016-03-14 08:17:11 -0400 Processing by ArticlesController#index as HTML Parameters: {"search"=&gt;"asd", "utf8"=&gt;"✓"} Article Load (0.1ms) SELECT "articles".* FROM "articles" Rendered articles/index.html.erb within layouts/application (1.4ms) Completed 200 OK in 15ms (Views: 14.1ms | ActiveRecord: 0.1ms) 看起来搜索实际上并不是在搜索,我错过了什么?
  • 我目前正在数据库中搜索一个名为 asd 的项目,但是,当我搜索它时,我得到了上述消息(在服务器的后端),以及整个列表对于数据库中的内容,它看起来像是列出了文章数据库中的所有内容,而不仅仅是搜索项目或搜索项目以及数据库的其余部分。
  • 这里是数据库代码class CreateArticles &lt; ActiveRecord::Migration def change create_table :articles do |t| t.string :title t.text :text t.timestamps null: false end end end
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2015-10-01
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    相关资源
    最近更新 更多