0
我試圖實現一個簡單的搜索函數來顯示任何匹配的帖子,但我得到錯誤「SQLite3 :: SQLException:沒有這樣的柱:內容:SELECT 「職位」 * FROM 「職位」 WHERE(內容LIKE '%PO%' OR名LIKE '%PO%')ORDER BY created_at DESC」當我嘗試搜索帖子時獲取「SQLite3 :: SQLException:no such column:content:」
控制器:
class PostsController < ApplicationController
def index
@posts = Post.all
if params[:search]
@posts = Post.search(params[:search]).order("created_at DESC")
else
@posts = Post.all.order('created_at DESC')
end
end
def edit
@posts = Post.find(params[:id])
end
def update
@posts = Post.find(params[:id])
if @posts.update(posts_params)
redirect_to @posts
else
render 'edit'
end
end
def new
@posts = Post.new
end
def create
@posts = Post.new(posts_params)
if @posts.save
redirect_to @posts
else
render 'new'
end
end
def show
@posts = Post.find(params[:id])
end
private
def posts_params
params.require(:post).permit(:title, :description)
end
end
索引頁,其中的搜索是:
<p class="btn-minimal"><%= link_to 'Meu Blog', controller: 'posts' %></p>
</br>
<p class ="btn-minimal"><%= link_to 'New post', new_post_path %></p>
</br></br>
<h1>Lista de posts</h1>
<%= form_tag(posts_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search Posts" %>
<%= submit_tag "Search" %>
<% end %>
<table class="table table-bordered table-hover table-condensed" align="center">
<tr>
<th>Title</th>
<th>Text</th>
</tr>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.description %></td>
<td class="btn-minimal"><%= link_to 'Show', post_path(post) %></td>
<td class="btn-minimal"><%= link_to 'Edit', edit_post_path(post) %></td>
</tr>
<% end %>
</table>
</body>
post.rb模型文件:
class Post < ActiveRecord::Base
validates :title,
:presence => {:message => "Title can't be blank." },
:uniqueness => {:message => "Title already exists."},
length: { minimum: 5 }
validates :description,
:presence =>{:message => "Description can't be blank." }
def self.search(search)
where("content LIKE ? OR name LIKE?" , "%#{search}%", "%#{search}%")
end
end
有人能告訴我最新錯了嗎?
錯誤表示您的「Posts」表中沒有「內容」列。看看你的'db/schema.rb'你的'Post'模型實際上具有哪些列。 – sugaryourcoffee
沒有列內容意味着你沒有在你的數據庫表中的列 - 我想你想要描述? ie'where(「description LIKE?or title LIKE?」,「%#{search}%」,「%#{search}%」)' - 也許你從某處粘貼搜索方法並且不會替換列名正常嗎? – house9
哦,謝謝,是的,我正在教一個教程來學習一些東西,我忘了把名字改成我給他們的名字,對一個小孩很抱歉,感謝你的幫助 – GaoVN