2016-04-19 59 views
4

我想弄清楚如何用Rails 4制作应用程序。我一直在基本的东西上停滞不前,而且我似乎无法确定使用未来的原则。如何定义链接路径

我有一个配置文件模型和一个行业模型。该协会是:

简介:

has_and_belongs_to_many :industries, join_table: 'industries_profiles' 

行业:

has_and_belongs_to_many :profiles, join_table: 'industries_profiles' 

在我的个人资料显示页面,我现在正在尝试链接到网页行业:

<% @profile.industries.limit(5).each do |industry| %> 

    <%= link_to industry.sector.upcase, industry_path(@industry) %> 

<% end %> 

我找不到适用于此链接的任何内容。

我曾尝试以下:

industry_path(@profile.industry) 
industry_path(@profile.industry_id) 
industry_path(industry) 
industry_path(profile.industry) 
industry_path(industry.id) 
industry_path(industry_id) 

但是所有的人都猜测。我不知道如何准备API码头,因此我无法理解它的任何内容。

任何人都可以看到如何链接到单个记录的HABTM协会的另一侧的显示页面?

+0

你有什么你的routes.rb文件? 'industry_path(行业)'是你应该使用的一个。 –

+0

我有:资源:行业 – Mel

+0

当我尝试的时候,当我将鼠标悬停在链接上时,它显示了一个具有正确行业ID的路径。但我不能点击它 - 没有任何反应 – Mel

回答

0

您可以通过在命令行中运行rake routes | grep industry来获取路由列表,该列表将为您提供一个包含前缀,操作和uri模式的表。例如:

industries GET /industries(.:format)   industries#index 
       POST /industries(.:format)   industries#create 
new_industry GET /industries/new(.:format)  industries#new 
edit_industry GET /industries/:id/edit(.:format) industries#edit 
    industry GET /industries/:id(.:format)  industries#show 
       PATCH /industries/:id(.:format)  industries#update 
       PUT /industries/:id(.:format)  industries#update 
       DELETE /industries/:id(.:format)  industries#destroy 

在你的情况,你应该看看show路径。哪个行业,你追加_path到底是什么你的前缀是上面,这是来自industry_path。而且,由于您在定义循环时已声明变量industry,因此可以使用该变量代替实例变量。

简短的回答:industry_path(industry)