简体   繁体   English

Ruby on Rails 中的数据库查询

[英]DB Query in Ruby on Rails

I am trying to perform a database search in an application, however, I have been unable to get the search to work properly.我正在尝试在应用程序中执行数据库搜索,但是,我一直无法使搜索正常工作。

In the index page I have the option to search or display all items in the database, however the return all always seems to be called, instead of the search.在索引页面中,我可以选择搜索或显示数据库中的所有项目,但是似乎总是调用return all ,而不是搜索。 If someone searches, I would like to only display the searched for item when the search is completed.如果有人搜索,我只想在搜索完成时显示搜索到的项目。

I am, however, running into some issues.但是,我遇到了一些问题。

articles_controller.rb:文章_controller.rb:

class ArticlesController < ApplicationController
  def index
    if params[:id]
      fun = Article.find_by(title: params[:id].search)
    else
      @articles = Article.all
    end
  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:文章.rb:

class Article < ActiveRecord::Base

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

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

end

index.html.erb: index.html.erb:

<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", :title => nil %>
     </p>
 <% end %>

</table>

Backend DB:后端数据库:

Started GET "/articles?search=asdfa&commit=Search&utf8=%E2%9C%93" for ::1 at 2016-03-14 15:01:12 -0400
Processing by ArticlesController#index as HTML
  Parameters: {"search"=>"asdfa", "commit"=>"Search", "utf8"=>"✓"}
  Article Load (0.1ms)  SELECT "articles".* FROM "articles"
  Rendered articles/index.html.erb within layouts/application (1.4ms)
Completed 200 OK in 19ms (Views: 18.6ms | ActiveRecord: 0.1ms)

The latest error/incorrect call is:最新的错误/不正确的调用是:

Started GET "/assets/application.self-3b8dabdc891efe46b9a144b400ad69e37d7e5876bdc39dee783419a69d7ca819.js?body=1" for ::1 at 2016-03-14 15:39:25 -0400


Started GET "/articles?search=asdfa&commit=Search&utf8=%E2%9C%93" for ::1 at 2016-03-14 15:39:40 -0400
Processing by ArticlesController#index as HTML
  Parameters: {"search"=>"asdfa", "commit"=>"Search", "utf8"=>"✓"}
  Article Load (0.1ms)  SELECT "articles".* FROM "articles"
  Rendered articles/index.html.erb within layouts/application (1.3ms)
Completed 200 OK in 18ms (Views: 17.2ms | ActiveRecord: 0.1ms)

What exactly am I doing wrong?我到底做错了什么? How can I fix/make this work?我怎样才能修复/使这项工作?

You've defined search method in your model, but you are not using in it in your controller:您已经在模型中定义了search方法,但没有在控制器中使用它:

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

In your model, you have some logic errors (symbol :search instead of search variable, tite instead of title ):在您的模型中,您有一些逻辑错误(符号:search而不是search变量, tite而不是title ):

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

In your controller you have:在您的控制器中,您有:

def index
    if params[:id]
      fun = Article.find_by(title: params[:id].search)
    else
      @articles = Article.all
    end
end

but you are definitely not passing any id to the controller via the form.但您绝对不会通过表单将任何 id 传递给控制器​​。

For example, you can change the condition to:例如,您可以将条件更改为:

if params[:search]

and then find by:然后通过以下方式查找:

Article.find_by(title: params[:search])

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

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