Ho una tabella di "post" con attributi che includono title
e body
.Cerca in Rails
posts_controller.rb:
class PostsController < ApplicationController
def index
@posts = Post.search(params[:search], params[:id])
end
end
index.html.erb:
<%= form_tag posts_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search]%>
<%= submit_tag "Search", :name => nil %>
<% end %>
<hr />
<table>
<tr>
<th>Title</th>
<th>Text</th>
</tr>
<tr>
<td><hr></td>
<td><hr></td>
</tr>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', :action => :show, :id => post.id %></td>
<td><%= link_to 'Edit', :action => :edit, :id => post.id %></td>
<td><%= link_to 'Destroy', { :action => :destroy, :id => post.id }, :method => :delete, :confirm => 'Are you sure?' %></td>
</tr>
<tr>
<td><hr></td>
<td><hr></td>
</tr>
<% end %>
</table>
e post.rb
def self.search(search, id)
if search
where(['name LIKE ?', "%#{search}%"])
else
scoped
end
end
Quando presento i params di ricerca, ottengo un errore messaggio:
ActiveRecord::StatementInvalid in Posts#index
SQLite3::SQLException: no such column: name: SELECT "posts".* FROM "posts" WHERE (name LIKE '%lorem%')
Extracted source (around line #23):
23: <% @posts.each do |post| %>
APD: Voglio cercare per 'titolo'.
Se si desidera cercare per titolo, perché si utilizza una colonna 'name', che apparentemente non esiste? –
Dovresti postare il tuo commento come risposta dato che qui è sicuramente il problema;) – Robin
Dave ThankYou !!! =))) –