【发布时间】: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