Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 呈现关联数据(有很多,:通过)-Rails_Ruby On Rails_Model_Controller_Associations_Render - Fatal编程技术网

Ruby on rails 呈现关联数据(有很多,:通过)-Rails

Ruby on rails 呈现关联数据(有很多,:通过)-Rails,ruby-on-rails,model,controller,associations,render,Ruby On Rails,Model,Controller,Associations,Render,我有两个导航栏(placement&ad),用于定义页面内容(adtype)。第一个导航栏placement始终在顶部可见。第二个导航栏ad将根据在位置顶部选择的属性呈现在页面左侧。模特放置和模特广告有多对多的关系。如何根据顶部导航栏placement中选择的属性,在html中呈现第二个导航ad 有两种导航模式: class Placement < ActiveRecord::Base attr_accessible :placementname has_many :adt

我有两个导航栏(placement&ad),用于定义页面内容(adtype)。第一个导航栏placement始终在顶部可见。第二个导航栏ad将根据在位置顶部选择的属性呈现在页面左侧。模特放置和模特广告有多对多的关系。如何根据顶部导航栏placement中选择的属性,在html中呈现第二个导航ad

有两种导航模式:

class Placement < ActiveRecord::Base  
  attr_accessible :placementname  
  has_many :adtypes, :foreign_key => "placement_id"  
  has_many :ads, :through => :adtypes   
end


class Ad < ActiveRecord::Base  
  attr_accessible :adname  
  has_many :adtypes, :foreign_key => "ad_id"  
  has_many :placements, :through => :adtypes  
end
这是我的routes.rb文件:

  resources :placements do  
    resources :ads do  
      resources :adtypes do  
      end  
    end  
  end  
下面是我如何呈现顶部导航(_navigation.html.erb)并将每个属性链接到以位置id结尾的新url(http://localhost:3000/placements/2)



  • 找到了解决方案:首先,我错误地将它们作为复数:

    belongs_to :placements 
    belongs_to :ads 
    
    因此,我刚刚去掉了“s”,并以这种方式呈现了我的广告(ads view show):

    
    
    哪些控制器操作需要呈现广告导航栏?它只是
    placements#show
    还是应该是整个网站的标准模板?应该是整个网站的标准模板。虽然我刚刚找到了解决办法。首先,我错误地将它们复数为:belies to:placements belies to:ads,所以我刚刚去掉了“s”,并以这种方式呈现了我的广告(ads view show):
  • 感谢您的关注。对不起,我本该抓到的。您可能希望创建一个答案并接受它。
      resources :placements do  
        resources :ads do  
          resources :adtypes do  
          end  
        end  
      end  
    
    <% Placement.all.each do |placement| %>  
      <li ><%= link_to placement.placementname, placement_path(placement.id) %> </li>  
    <%end%>  
    
    belongs_to :placements 
    belongs_to :ads 
    
    <% @placement.ads.all.each do |ad| %>  
    <%= link_to ad.adname, placement_ad_path(@placement, ad) %>  
    <%end%>