简体   繁体   English

在Ruby-on-Rails数据库中搜索字段

[英]Searching a field in Ruby-on-Rails DB

I currently have a DB in Ruby on Rails, however, I have been having trouble with the documentation on how to do much other than list all of the items in DB. 我目前在Ruby on Rails中拥有一个数据库,但是,除了列出数据库中的所有项目外,我在如何执行更多工作的文档方面遇到了麻烦。 I am still new to the this language as a whole, and wish I didn't need to ask for so much help, but here it goes. 我仍然对这门语言整体还是陌生的,希望我不需要寻求太多帮助,但是就可以了。 My pertinent code is as follows: 我的相关代码如下:

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>

Thanks for any help in advance! 感谢您的任何帮助!

Essentially your issue is that you're trying to set controller instance variables in a class method in your model. 本质上,您的问题是您试图在模型的类方法中设置控制器实例变量。

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

article.rb 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

So now the class method does the search, returns the collection to the controller which assigns them to an instance variable for use in the view. 因此,现在class方法进行搜索,将集合返回给控制器,该控制器将它们分配给实例变量以供在视图中使用。

You have got your search form, made sure that is a GET one. 您已经有了搜索表,请确保该表是一个GET表单。 Good. 好。 When you hit search you'll notice in your development log that there's a hit to articles#index and the browser will show the same as before. 当您点击搜索时,您会在开发日志中注意到对articles#index ,浏览器将显示与以前相同的内容。 To make the search matter edit the index method in the articles controller. 要使搜索事项编辑文章控制器中的index方法。

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

In Article.search you have a name where you should have a title . Article.search您有一个name ,其中应该有一个title

PS: You've got the show method a bit wrong. PS:您的show方法有点错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM