当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby on rails - Link To Parent From Nested Resource

问题描述:

How on earth do I link up to a parent resource? My Companies have many Orders.

In my orders view, I have listed the associated company with

@order.company.name

However, when I try and use link_to, it links only to the order:

<%= link_to @order.company.name, company_path %>

In my routes, I have this:

 resources :companies do

resources :orders do

resources :comments

end

end

What's the fix and where can I read more about it?

网友答案:

<%= link_to @order.company.name, company_path(@order.company) %>

You have to pass something to company_path so that it knows which Company to get the path for. That is distinct form companies_path which returns the path for all the companies.

/companies/12 vs /companies

网友答案:

I guess you have a model orders and a model companies. so companies has_many orders. If you setup this with the usual way your records are all have id as identifier. In this case company.id is the record id if you want to make a link back to the company page.

分享给朋友:
您可能感兴趣的文章:
随机阅读: