开发者

Create a view using multiple models data

开发者 https://www.devze.com 2023-02-12 18:49 出处:网络
My (rails 3) application use 开发者_运维技巧collection and product as models. collection has_many products and product belongs_to collection.

My (rails 3) application use 开发者_运维技巧collection and product as models. collection has_many products and product belongs_to collection.

I managed to have interactions between products and models. I created a menu displaying the different collection. I want to display a view showing only the product belonging to a specific collection.

1) Is it more elegant to create a new view/controller, or do i create a new view in the product views

2)It seems that i must do something with the routes.rb, but how and what?

3)What link_to arguments must i use to pass the value of my collection?

4)I read a whole book (pragmatic ROR) and depspite that and doing a lot of online research i keep ending here asking for not so much complicated Rails question. What am i doing wrong?


  1. I would go with creating new action in collection controllers. Url will look like this:

    /collections/1/products

    where 1 is collection id.

  2. I assume you have

    resources collections

    so you need to add 'products' action for collection member:

    resources collections do
      member do
        get :products
      end
    end
    

    You can run rake routes from console to see how your application routes look now.

  3. Link code should look like this

    link_to "Collection products", products_collection_path(@collection)
    
  4. In my opinion reading is ok, but while you read you should do lots of examples, write them yourself, becouse otherwise you forget stuff very quickly. I'm 100% sure that stuff I wrote above was in the book you've read.

0

精彩评论

暂无评论...
验证码 换一张
取 消