2016-03-14 129 views
0

我目前在Ruby on Rails中有一个数据库,但是,我一直在关于如何做其他事情的文档上遇到问题,而不是列出数据库中的所有项目。我对整个这种语言还是陌生的,并且希望我不需要这么多的帮助,但是在这里它就是这样。我的相关代码如下:在Ruby on Rails中搜索字段DB

migrate/(DB name)

class CreateArticles < ActiveRecord::Migration 
    def change 
    create_table :articles do |t| 
     t.string :title 
     t.text :text 

     t.timestamps null: false 
    end 
    end 
end 

articles_controller.rb

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

    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.where('name LIKE ?', "%#{search}%") 
    else 
     @article = Article.all 
    end 
    end 

end 

index.html.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> 

感谢您提前提供任何帮助!

回答

1

本质上你的问题是你试图在模型中的类方法中设置控制器实例变量。

class ArticlesController < ApplicationController 
    def index 
    @articles = Article.search(params[:search]) 
    end 
end 

article.rb

class Article < ActiveRecord::Base 

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

    def self.search(search) 
    if search.present? 
     Article.where('title LIKE ?', "%#{search}%") 
    else 
     Article.all 
    end 
    end  
end 

所以,现在的类的方法进行搜索,收集返回到它们分配给一个实例变量对于在视图使用该控制器。

+0

ha,你错过了“名称”;) –

+0

真的,刚从OP的问题 –

+0

复制出来,现在我收到了'参数:{“search”=>“asd”,“utf8”=>“✓” } 文章加载(0.5ms)选择“文章”。*从后端的“文章”'在前端没有变化 –

0

你有你的搜索表单,确保这是一个GET。好。当您点击搜索时,您会注意到您的开发日志中存在articles#index,并且浏览器将显示与以前相同的内容。要使搜索内容在文章控制器中编辑index方法。

def index 
    @articles = Article.all.search(params[:search]) 
end 

Article.search你有一个name,你应该有一个title

PS:你有show方法有点不对。

+0

谢谢,不好看看秀,但是在搜索方面,在后台我还是没有看到调用时选择正确的''参数:{“search”=>“asd”,“utf8”=>“✓”} 文章载入(0.1ms)SELECT“articles”。* FROM“articles”'是什么我从后台收到 –

+0

嗯,可能是'Article.search(params [:搜索])'然后像j-dexx建议的那样。我的mongoid对“.all”没有太多打扰。 –

+0

不,它仍然看到搜索,但只运行'SELECT“条目。* FROM”articles“ –