尝试向我的应用添加简单搜索功能,以便任何人都可以搜索文章。添加代码后,它不会在索引上显示搜索结果。但该网址显示http://localhost:3000/articles?utf8=%E2%9C%93&search=hello&commit=Search
index.html.erb
<p>This is the articles placeholder</p>
<%= form_tag articles_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search" %>
<% end %>
<% @articles.each do |article| %>
<h2><%= link_to article.title, article %></h2>
<p>Published at
<%= article.created_at.strftime('%b %d, %Y') %>
</p>
<p>
<%= truncate(article.content, length: 200) %>
</p>
<% end %>
文章控制器
class ArticlesController < ApplicationController
before_action :find_article, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@articles = Article.search(params[:search])
@articles = Article.all.order("created_at DESC")
end
def new
@article = current_user.articles.build
end
def show
end
def create
@article = current_user.articles.build(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def edit
end
def update
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article.destroy
redirect_to root_path
end
private
def article_params
params.require(:article).permit(:title, :content)
end
def find_article
@article = Article.find(params[:id])
end
end
article.rb
class Article < ActiveRecord::Base
belongs_to :user
def self.search(search)
if search
where(["name LIKE ?", "%#{search}%"])
else
all
end
end
end
rake routes
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
root GET / articles#index
答案 0 :(得分:1)
您将覆盖搜索,然后调用Article.all.order("created_at DESC")
并将其分配给@articles
变量
@articles = Article.search(params[:search])
@articles = Article.all.order("created_at DESC")
所以你想要
@articles = Article.search(params[:search]).order("created_at DESC")
因为如果参数不存在,你的搜索方法将全部返回。