On Aug 27, 2009, at 8:54 AM, hansfh wrote:
> Hi,
> im a rails newbie, so please be gentle:
>
> let's say category habtm products and i've got a product with the id
> 7.
>
> when i use the url
>
> /categories/7/products
>
> should the server show me only the categories of product 7?
>
> On my Server it always shows all categories i have.
>
> How do i solve this the right way?
>
> I edited the routes.rb so, that
>
>  map.resources :categories, :has_many => [:products]
>
> But this does not change anythings, using
> /categories/7/products always leads to the index action of the
> products controller.
>
> Is the right way to test in the index action of the products
> controller whether category_id is set in params and use this
> information to only show products of this category? Or is there a
> better way?
>
> Thanks,
>
> HansFH


If you have a line in ProductsController#index like:

   @products = Product.find(:all)

Then of course you'll see all products. The question is what you want  
to see at that url. I'd expect to see all the products in category 7

   def index
     if find_category
       @products = @category.products
     else
       flash[:error] = "No category with id=#{params[:category_id]}"
       redirect_to categories_path
     end
   end
   def find_category
     @category = Category.find_by_id(params[:category_id])
   end

And you can even put the find_category into a before_filter and  
redirect from it directly.

-Rob

Rob Biedenharn          http://agileconsultingllc.com
[email protected]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to