1

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>'
5
  • have you tried .search in rails console? the log doesn't seem like it actually performed the where Commented Aug 30, 2016 at 17:00
  • Please add the partial for completeness as well. Commented Aug 30, 2016 at 18:13
  • the partial is working, otherwise i wouldnt get the full list of items, the problem is on the query not passing Commented Aug 30, 2016 at 18:31
  • I added the output when i try to use search in the console Commented Aug 30, 2016 at 18:34
  • @frenciaj, I have added my answer. Let me know if it resolves your issue. Commented Aug 30, 2016 at 22:03

1 Answer 1

1

Are you sure you defined the class method search in item.rb? Cause the error says that there's no search method defined on the Item class. However, try if the following works for you.

You only want to search if the form is being submitted. You can use the present? method to know if the search term was actually passed.

Modify your controller to

def index
  @items = params[:search].present? ? Item.search(params[:search]) : Item.all
end

Note that an object is present only if its not blank. This is handy because if someone enters " " in the form, you shouldn't search for an empty space in the nombre field but display all the records in the items table.

If the form was not submitted, Item.all will be executed.

Modify your class method to

def self.search(search_term)
  where("nombre LIKE ?", "%#{search_term}%")
end

You don't need a Item.where because the class Item is the current context on which the where method is called. Note that I have changed the pattern to %#{search_term}% cause you don't always want to match strings ending with some pattern.

Hope this helps!

Sign up to request clarification or add additional context in comments.

11 Comments

I modify this based on your answer, but its still not getting the query, the log shows the same as i had previously
@frenciaj, Thats really strange. Were you able to do Item.search(term) in the rails console? What do you have you in your index action? Looks like you are accessing a user's items and limiting them to 50 records. Please post your latest code in the index action.
Items controller def index @items = params[:search].present? ? Item.search(params[:search]) : Item.all end the user part is because devise is installed for authentication
@frenciaj, if you have a look at the logs, you can see that limit and sort are done on the records. Were you able to do Item.search(term)? in the console?
no, i cant i get the following error: NoMethodError: undefined method `search' for #<Class:0x0000000203fc58>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.