首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rails 4.2:无法添加搜索功能。

Rails 4.2:无法添加搜索功能。
EN

Stack Overflow用户
提问于 2015-11-16 11:52:25
回答 1查看 48关注 0票数 1

尝试在我的应用程序中添加简单的搜索功能,这样任何人都可以搜索文章。添加代码后,它不会在索引上显示搜索结果。但是url显示http://localhost:3000/articles?utf8=%E2%9C%93&search=hello&commit=Search

index.html.erb

代码语言:javascript
运行
复制
<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 %>

物品控制器

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
class Article < ActiveRecord::Base
  belongs_to :user

  def self.search(search)
    if search
      where(["name LIKE ?", "%#{search}%"])
    else
      all
    end
  end
end

耙路

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-16 12:00:23

您将覆盖搜索,然后调用Article.all.order("created_at DESC")并将其分配给@articles变量

代码语言:javascript
运行
复制
@articles = Article.search(params[:search])
@articles = Article.all.order("created_at DESC")

所以你想

代码语言:javascript
运行
复制
@articles = Article.search(params[:search]).order("created_at DESC")

因为如果参数不存在,搜索方法将返回所有参数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33734736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档