[英]DB Query in Ruby on Rails
我正在嘗試在應用程序中執行數據庫搜索,但是,我一直無法使搜索正常工作。
在索引頁面中,我可以選擇搜索或顯示數據庫中的所有項目,但是似乎總是調用return all
,而不是搜索。 如果有人搜索,我只想在搜索完成時顯示搜索到的項目。
但是,我遇到了一些問題。
文章_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
文章.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:
<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>
后端數據庫:
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)
最新的錯誤/不正確的調用是:
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)
我到底做錯了什么? 我怎樣才能修復/使這項工作?
您已經在模型中定義了search
方法,但沒有在控制器中使用它:
def index
@articles = Article.search params[:search]
end
在您的模型中,您有一些邏輯錯誤(符號:search
而不是search
變量, tite而不是title
):
def self.search search
search.present? ? where('title LIKE ?', "%#{search}%") : all
end
在您的控制器中,您有:
def index
if params[:id]
fun = Article.find_by(title: params[:id].search)
else
@articles = Article.all
end
end
但您絕對不會通過表單將任何 id 傳遞給控制器。
例如,您可以將條件更改為:
if params[:search]
然后通過以下方式查找:
Article.find_by(title: params[:search])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.