2

ページのコンテンツ (adtype) を定義する 2 つのナビゲーション バー (プレースメントと広告) があります。最初のナビゲーション バーである配置は、常に上部に表示されます。2 つ目のナビゲーション バーである ad は、プレースメントの上部で選択された属性に基づいて、ページの左側に表示されます。モデルの配置とモデルの広告は、多対多の関係にあります。上部のナビゲーション バーで選択されている属性に基づいて、2 番目のナビゲーションである広告を HTML でレンダリングするにはどうすればよいですか?

2 つのナビゲーション モデルは次のとおりです。

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

以下は、ナビゲーション バーで選択された属性に依存するコンテンツ adtype のモデルです。

class Adtype < ActiveRecord::Base  
  attr_accessible :adtype_screenshot, :location, :placement_screemshot, :specs  
  belongs_to :placements  
  belongs_to :ads  
end

ここに私のスキーマデータベースがあります:

ActiveRecord::Schema.define(:version => 20121127052112) do  
  create_table "ads", :force => true do |t|  
    t.string   "adname"  
    t.datetime "created_at"  
    t.datetime "updated_at"  
  end 

  create_table "adtypes", :force => true do |t|  
    t.integer  "placement_id"  
    t.integer  "ad_id"  
    t.string   "location"  
    t.string   "placement_screenshot"  
    t.string   "adtype_screenshot"  
    t.string   "specs"  
    t.datetime "created_at"  
    t.datetime "updated_at"  
  end

  create_table "placements", :force => true do |t|  
    t.string   "placementname"  
    t.datetime "created_at"  
    t.datetime "updated_at"  
  end    
end  

ここに私のroutes.rbファイルがあります:

  resources :placements do  
    resources :ads do  
      resources :adtypes do  
      end  
    end  
  end  

トップ ナビゲーション (_navigation.html.erb) をレンダリングし、配置 ID (http://localhost:3000/placements/2) で終わる新しい URL に各属性をリンクする方法を次に示します。

<% Placement.all.each do |placement| %>  
  <li ><%= link_to placement.placementname, placement_path(placement.id) %> </li>  
<%end%>  
4

1 に答える 1

0

解決策を見つけました: 最初に、これらを誤って複数形として持っていました:

belongs_to :placements 
belongs_to :ads 

そのため、「s」を削除して、広告を次のようにレンダリングしました (広告ビュー ショー):

<% @placement.ads.all.each do |ad| %>  
<%= link_to ad.adname, placement_ad_path(@placement, ad) %>  
<%end%>
于 2012-12-28T20:00:50.017 に答える