Search function fails because it refers to the wrong controller action?

Posted by Christoffer on Stack Overflow See other posts from Stack Overflow or by Christoffer
Published on 2012-06-24T13:03:50Z Indexed on 2012/06/24 15:16 UTC
Read the original article Hit count: 255

My Sunspot search function (sunspot_rails gem) works just fine in my index view, but when I duplicate it to my show view my search breaks...

views/supplierproducts/show.html.erb

<%= form_tag supplierproducts_path, :method => :get, :id => "supplierproducts_search" do %>
  <p>
    <%= text_field_tag :search, params[:search], placeholder: "Search by SKU, product name & EAN number..." %>
  </p>
  <div id="supplierproducts"><%= render 'supplierproducts' %></div>
<% end %>

assets/javascripts/application.js

$(function () {
  $('#supplierproducts th a').live('click',
    function () {
      $.getScript(this.href);
      return false;
    }
  );

  $('#supplierproducts_search input').keyup(function () {
    $.get($("#supplierproducts_search").attr("action"), $("#supplierproducts_search").serialize(), null, 'script');
    return false;
  });
});

views/supplierproducts/show.js.erb

$('#supplierproducts').html('<%= escape_javascript(render("supplierproducts")) %>');

views/supplierproducts/_supplierproducts.hmtl.erb

<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<table class="table table-bordered">
  <thead>
    <tr>
      <th><%= sortable "sku", "SKU" %></th>
      <th><%= sortable "name", "Product name" %></th>
      <th><%= sortable "stock", "Stock" %></th>
      <th><%= sortable "price", "Price" %></th>
      <th><%= sortable "ean", "EAN number" %></th>
    </tr>
  </thead>
    <% for supplierproduct in @supplier.supplierproducts %>
  <tbody>
    <tr>
      <td><%= supplierproduct.sku %></td>
      <td><%= supplierproduct.name %></td>
      <td><%= supplierproduct.stock %></td>
      <td><%= supplierproduct.price %></td>
      <td><%= supplierproduct.ean %></td>
    </tr>
  </tbody>
  <% end %>
</table>

controllers/supplierproducts_controller.rb

class SupplierproductsController < ApplicationController

helper_method :sort_column, :sort_direction

  def show
    @supplier = Supplier.find(params[:id])
    @search = @supplier.supplierproducts.search do
      fulltext params[:search]
    end
    @supplierproducts = @search.results
  end
end

private

  def sort_column
    Supplierproduct.column_names.include?(params[:sort]) ? params[:sort] : "name"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ?  params[:direction] : "asc"
  end

models/supplierproduct.rb

class Supplierproduct < ActiveRecord::Base
  attr_accessible :ean, :name, :price, :sku, :stock, :supplier_id
  belongs_to :supplier

  validates :supplier_id, presence: true

  searchable do
    text :ean, :name, :sku
  end
end

Visiting show.html.erb works just fine. Log shows:

Started GET "/supplierproducts/2" for 127.0.0.1 at 2012-06-24 13:44:52 +0200
Processing by SupplierproductsController#show as HTML
  Parameters: {"id"=>"2"}
  Supplier Load (0.1ms)  SELECT "suppliers".* FROM "suppliers" WHERE "suppliers"."id" = ? LIMIT 1  [["id", "2"]]
  SOLR Request (252.9ms)  [ path=#<RSolr::Client:0x007fa5880b8e68> parameters={data: fq=type%3ASupplierproduct&start=0&rows=30&q=%2A%3A%2A, method: post, params: {:wt=>:ruby}, query: wt=ruby, headers: {"Content-Type"=>"application/x-www-form-urlencoded; charset=UTF-8"}, path: select, uri: http://localhost:8982/solr/select?wt=ruby, open_timeout: , read_timeout: } ]
  Supplierproduct Load (0.2ms)  SELECT "supplierproducts".* FROM "supplierproducts" WHERE "supplierproducts"."id" IN (1)
  Supplierproduct Load (0.1ms)  SELECT "supplierproducts".* FROM "supplierproducts" WHERE "supplierproducts"."supplier_id" = 2
  Rendered supplierproducts/_supplierproducts.html.erb (2.2ms)
  Rendered supplierproducts/show.html.erb within layouts/application (3.3ms)
  Rendered layouts/_shim.html.erb (0.0ms)
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'zMrtTbDun2MjMHRApSthCQ' LIMIT 1
  Rendered layouts/_header.html.erb (2.1ms)
  Rendered layouts/_footer.html.erb (0.2ms)
Completed 200 OK in 278ms (Views: 20.6ms | ActiveRecord: 0.6ms | Solr: 252.9ms)

But it breaks when I type in a search. Log shows:

Started GET "/supplierproducts?utf8=%E2%9C%93&search=a&direction=&sort=&_=1340538830635" for 127.0.0.1 at 2012-06-24 13:53:50 +0200
Processing by SupplierproductsController#index as JS
  Parameters: {"utf8"=>"?", "search"=>"a", "direction"=>"", "sort"=>"", "_"=>"1340538830635"}
  Rendered supplierproducts/_supplierproducts.html.erb (2.4ms)
  Rendered supplierproducts/index.js.erb (2.9ms)
Completed 500 Internal Server Error in 6ms

ActionView::Template::Error (undefined method `supplierproducts' for nil:NilClass):
    10:       <th><%= sortable "ean", "EAN number" %></th>
    11:     </tr>
    12:   </thead>
    13:     <% for supplierproduct in @supplier.supplierproducts %>
    14:   <tbody>
    15:     <tr>
    16:       <td><%= supplierproduct.sku %></td>
  app/views/supplierproducts/_supplierproducts.html.erb:13:in `_app_views_supplierproducts__supplierproducts_html_erb___2251600857885474606_70174444831200'
  app/views/supplierproducts/index.js.erb:1:in `_app_views_supplierproducts_index_js_erb___1613906916161905600_70174464073480'


  Rendered /Users/Computer/.rvm/gems/ruby-1.9.3-p194@myapp/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (33.3ms)
  Rendered /Users/Computer/.rvm/gems/ruby-1.9.3-p194@myapp/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms)
  Rendered /Users/Computer/.rvm/gems/ruby-1.9.3-p194@myapp/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (39.7ms)

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby-on-rails-3