如何从Rails 4的父元素外部链接到嵌套子元素? [英] How can I link to a nested child from outside the parent element in Rails 4?
问题描述
好的,我的情况.我有
resources :projects do
resources :campaigns
end
我想使用
resources :portfolios
我想制作一个<%link_to%>,列出我可以做的所有广告系列,但是当我尝试路由路径时,我无法使其链接到广告系列.
I want to make a <% link_to %> that listed all campaigns which I can do, but when I try to route the path i can't make it link to a campaign.
示例:在我的 portfolios/view/index.html.erb
<% @projects.each do |p| %>
<% p.campaigns.each do |c| %>
<div class="container" >
<div>
<%= link_to ( image_tag c.image_camp.url(:thumb)), p %>
</div>
<div class="col-md-2">
<h3 style=""><%=link_to c.title, p %></h3>
<h4><%= c.end_date %></h4>
<h4><%= c.project_end %></h4>
</div>
<div class="col-md-6">
<h4><%= c.project_end %></h4>
<p><%= link_to "read more", p %></p>
</div>
</div>
<% end %>
<% end %>
上面的示例链接到@project,我试图将'c'希望链接到我显然是从中获取信息的@campaign,但是当我收到错误消息时
The above example links to the @project, I have tried putting 'c' in hopes it will link to the @campaign that I am obviously getting information from, but when I do I get the error
undefined method `campaign_path' for #<#<Class:0x0000000cc74b68>:0x0000000ef964c0>
我还尝试了project_campaigns_path(@project,campaign)和我能想到的所有其他组合.
I've also tried project_campaigns_path(@project, campaign) and every other combination I could think of.
undefined local variable or method `campaign' for #<#<Class:0x0000000cc74b68>:0x0000000ae94cd8>
我觉得问题出在Portfolios_controller上,我还没有了解控制器如何影响路线以及如何访问这些变量.
I feel the problem lies in the portfolios_controller and that I have yet to understand how the controller affects routes and how to access those variables.
class PortfoliosController < ApplicationController
def index
@projects = Project.all.order('created_at DESC').includes(:campaigns)
end
end
我假设找到了@projects和@campaigns,因为我能够在项目组合/视图中显示所有内容,我想念什么?
I assumed that @projects and @campaigns are both found as I am able to display everything on the portfolio/view, what am I missing?
推荐答案
您需要将Project
和Campaign
传递给link_to
.然后它将知道如何路由到嵌套资源.
You need to pass the Project
and the Campaign
to the link_to
. It will then know how to route to the nested resource.
这篇关于如何从Rails 4的父元素外部链接到嵌套子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!