I am trying to create an on line store and i would like to have the shopper select by category. i have established the many to many relationship between product and category, but I don't believe that I am calling it out in the controller properly. What should I be using in the index action in the category controller to recognize products?
Main Topics
Browse All Topics





by: wesgarrisonPosted on 2009-10-27 at 06:59:13ID: 25672471
If you would, post your models with the relationship (no need for the entire thing if you have validations and other stuff). Just to double check on 'em.
egory_id). products) than to try to find it from the products side.
First: filtering by category
From reading your code it looks like you want to put a dropdown that filters the list of products by category, right?
Here's an overview of how I do it for you to try and then you can ask if you have questions.
* Put the product table in a partial
* Change the view to use the partial
* Add the observer like you've done
* In the Ajax responding action (change_filter for you) select the appropriate items with a find() statement. It'll be easier to scope through the category (Category.find(params[:cat
* render the partial with those records
* the observer will replace the table with the one you rendered
If you're having problems, step through it:
* does the partial work on the first page load?
* watch the log: is the observer firing and making a request? Maybe having multiple category dropdowns with the same id is breaking it or the category id isn't being passed.
* Check the log for the filtered-by-category query
The other issue you mentioned: "to display by category"
Remember that product.categories is going to return an Array, so you need to iterate over that and display or whatever you want to do for each category for that product.
If all you want to do it just display the categories, something like this should work:
Select allOpen in new window