Rails: 显示关联模型的属性
Rails: showing an attribute from an associated model
我正在尝试用 Rails 4.
制作一个应用
我有一个个人资料显示页面,其中应包含个人资料所属大学的名称。
我的个人资料中有这个 link#show:
<%= link_to @profile.university.name do %>
<span class="profiletitle"> <%= @profile.university.name %></span>
<% end %>
我有个人资料模型和大学模型。这些协会是:
profile: belongs_to :university
university: has_many :profiles
在我的大学里 table,我有一个名为 :name 的属性。
当我尝试这个时,我收到一条错误消息:
ActiveRecord::RecordNotFound at /profiles/University%20of%20Melbourne
Couldn't find Profile with 'id'=University of Melbourne
它指的是我的配置文件控制器,它有(在错误参考点所在的行):
def set_profile
@profile = Profile.find(params[:id])
end
我不明白为什么这个引用是相关的或者如何从个人资料显示中引用大学名称。我想让 ti 正常工作,所以您单击 link,它会将您带到 uni 概览显示页面。
如何引用关联模型中的属性?
我问了几个其他问题试图修复这个错误。最近的一个在这里。给出了一个广泛的答案,但我不希望像建议提供的那样设置关联,所以我迷失了如何跟随答案的内容。
谢谢
当我接受 Prashant 的建议并尝试时:
<%= link_to universities_path(@profile.university) do %>
<span class="profiletitle"> <%= @profile.university.name %></span>
<% end %>
我收到这个错误:
ActionController::UnknownFormat
它在我的大学控制器中引用了此操作:
def index
@universities = University.all
respond_with(@university)
end
我的个人资料路线是:
resources :profiles do
resources :account_histories
end
你的用法是这样的:
更新
<%= link_to university_path(@profile.university) do %>
<span class="profiletitle"> <%= @profile.university.name %></span>
<% end %>
你的 routes.rb 应该包含这个
resources :universities do
resources :profiles
# other stuff
end
end
我正在尝试用 Rails 4.
制作一个应用我有一个个人资料显示页面,其中应包含个人资料所属大学的名称。
我的个人资料中有这个 link#show:
<%= link_to @profile.university.name do %>
<span class="profiletitle"> <%= @profile.university.name %></span>
<% end %>
我有个人资料模型和大学模型。这些协会是:
profile: belongs_to :university
university: has_many :profiles
在我的大学里 table,我有一个名为 :name 的属性。
当我尝试这个时,我收到一条错误消息:
ActiveRecord::RecordNotFound at /profiles/University%20of%20Melbourne
Couldn't find Profile with 'id'=University of Melbourne
它指的是我的配置文件控制器,它有(在错误参考点所在的行):
def set_profile
@profile = Profile.find(params[:id])
end
我不明白为什么这个引用是相关的或者如何从个人资料显示中引用大学名称。我想让 ti 正常工作,所以您单击 link,它会将您带到 uni 概览显示页面。
如何引用关联模型中的属性?
我问了几个其他问题试图修复这个错误。最近的一个在这里。给出了一个广泛的答案,但我不希望像建议提供的那样设置关联,所以我迷失了如何跟随答案的内容。
谢谢
当我接受 Prashant 的建议并尝试时:
<%= link_to universities_path(@profile.university) do %>
<span class="profiletitle"> <%= @profile.university.name %></span>
<% end %>
我收到这个错误:
ActionController::UnknownFormat
它在我的大学控制器中引用了此操作:
def index
@universities = University.all
respond_with(@university)
end
我的个人资料路线是:
resources :profiles do
resources :account_histories
end
你的用法是这样的:
更新
<%= link_to university_path(@profile.university) do %>
<span class="profiletitle"> <%= @profile.university.name %></span>
<% end %>
你的 routes.rb 应该包含这个
resources :universities do
resources :profiles
# other stuff
end
end