Here what looks better, this is the log, its passing the param, but its not passing a query with that param
Started GET "/items?utf8=%E2%9C%93&search=blanca" for 65.34.251.106 at 2016-08-30 03:55:51 +0000
Processing by ItemsController#index as HTML
Parameters: {"utf8"=>"✓", "search"=>"blanca"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 3]]
Item Load (0.3ms) SELECT "items".* FROM "items" LIMIT 50 OFFSET 0
Item Load (0.2ms) SELECT "items".* FROM "items" ORDER BY "items"."id" ASC LIMIT 1 OFFSET 0
Rendered items/_items.html.erb (3.2ms)
Rendered items/index.html.erb within layouts/application (4.9ms)
This is the controller:
def index
@items = Item.search(params[:search])
end
and this is the model:
class Item < ActiveRecord::Base
has_many :stocks
attr_accessible :nombre, :espesor, :material, :quantity
accepts_nested_attributes_for :stocks
attr_accessible :stocks_attributes
self.per_page = 50
# def self.search(search)
# Item.where("nombre LIKE ?", "%#{search}%")
# end
def self.search(search_term)
where("nombre LIKE ?", "%#{search_term}%")
end
protected
end
and the search form in the view:
<%= form_tag items_path, :method => 'get', :id => "items_search" do %>
<p>
<%= text_field_tag :search, params[:search], class: "form-control" %>
<%= submit_tag "Search", :name => nil, class: "btn btn-danger" %>
</p>
<div id="items"><%= render 'items' %>
</div>
<% end %>
I have a _items.html.erb file which has the items to render, that part works because no matter my input on the search bar, it always shows all the items
This is the ouput when i try to use .search method in the console
2.3.0 :041 > Item.search("blanca")
NoMethodError: undefined method `search' for #<Class:0x0000000203fc58>
from /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord- 4.2.6/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
from (irb):41
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in `start'
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start'
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console'
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
.searchin rails console? the log doesn't seem like it actually performed thewhere