【问题标题】:How to define a link path如何定义链接路径
【发布时间】:2016-04-19 02:49:40
【问题描述】:

我正在尝试弄清楚如何使用 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 Dock,所以我无法理解它的任何内容。

任何人都可以看到如何链接到 HABTM 关联另一方的显示页面以获取单个记录吗?

【问题讨论】:

  • 你的 routes.rb 文件有什么? industry_path(industry) 是您应该使用的。
  • 我有:资源:行业
  • 当我尝试这样做时,当我将鼠标悬停在链接上时,它会显示一个包含正确行业 ID 的路径。但我无法点击它 - 没有任何反应
  • 正如 Ben 所说,industry_path(industry) 应该可以工作,你所说的“什么都没有发生”是什么意思?如果 href 上面有 id,那么它工作正常
  • 试试这个方法:

标签: ruby-on-rails ruby-on-rails-4 path associations link-to


【解决方案1】:

您可以通过在命令行中运行 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)

【讨论】:

    猜你喜欢
    • 2015-03-19
    • 1970-01-01
    • 2017-04-11
    • 2023-03-19
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 2016-03-28
    • 2021-05-25
    相关资源
    最近更新 更多