Hopefully this is a very easy problem to solve as I am just starting out with Rails.
I have one database (Products) which contains a number of product attributes including brand, colour and product_type.
I currently have an index page (within productfinder directory) which is able to provide a summary of the products in the database. I am using the following code in index.html.erb:
<% @distinctbrands.each do |distinctbrand| %>
<h4><%= distinctbrand %></h4>
<% end %>
Within productfinder_controller.rb i have:
def index
@distinctbrands = Product.find( :all, :order => 'brand ASC', :select => 'brand' ).map{ |i| i.brand }.uniq
@distinctcolours = Product.find( :all, :order => 'colour ASC', :select => 'colour' ).map{ |i| i.colour }.uniq
end
UP TO THIS POINT EVERYTHING SEEMS TO BE WORKING CORRECTLY
Now I would like to add 3 buttons onto my index page, these buttons would enable the user to recalculate the distinctbrands and distinctcolours for a subset of the products - matching either product_type = "new" or "used". The third button would correspond to the results not being filtered at all (i.e. as the index page loads initially).
In a previous question I was advised to add the following code: In productfinder_controller:
before_filter :load_products, :only => [:index, :bybrand, :bycolour, :byprice]
protected
def load_products
@products = Product.by_product_type(params[:product_type])
end
In Product.rb:
scope :by_product_type, lambda{ |product_type| where(product_type: product_type.to_i) unless product_type.nil? }
In index.html.erb
<%= link_to "New", :controller => params[:controller], :action => params[:action], :product_type => 'New' %>
<%= link_to "Used", :controller => params[:controller], :action => params[:action], :product_type => 'Used' %>
<%= link_to "Don't Restrict", :controller => params[:controller], :action => params[:action], :product_type => '' %>
When the application is now run I get a NoMethodError - ProductFinder#Index (while trying to do nil.each The error is associated with <% @distinctbrands.each do |distinctbrand| %>
I wondered if the problem was with the functions defining @distinctbrands and @distinctcolours as the database has now been filtered, however I get the same error for both the following situations:
def index
@distinctbrands = @product.find( :all, :order => 'brand ASC', :select => 'brand' ).map{ |i| i.brand }.uniq
@distinctcolours = @product.find( :all, :order => 'colour ASC', :select => 'colour' ).map{ |i| i.colour }.uniq
end
and
def index
@distinctbrands = Product.find( :all, :order => 'brand ASC', :select => 'brand' ).map{ |i| i.brand }.uniq
@distinctcolours = Product.find( :all, :order => 'colour ASC', :select => 'colour' ).map{ |i| i.colour }.uniq
end
Is somebody able to provide me with a possible solution please? Is the problem to do with @products not being defined within def index? If so, how can this be done?
Many thanks for your time
Thanks for all your help - I went through your suggestions again and managed to identify that I had a minor error in product.rb, now it reads:
All seems to be working well! Thanks again