[英]accessing the data from another model in ruby on rails
我有2個名為Subject&Page的模型。 他們之間有一對多的聯系。
class Subject < ActiveRecord::Base
has_many :pages
attr_accessible :name
attr_accessible :position
attr_accessible :visible
attr_accessible :created_at
end
和
class Page < ActiveRecord::Base
belongs_to :subject
attr_accessible :subject_id
attr_accessible :name
attr_accessible :permalink
attr_accessible :position
attr_accessible :visible
attr_accessible :created_at
end
我在view - > pages文件夾中有list.html.erb。
問:我的問題是我想在list.html.erb中顯示所有subject_id。 怎么樣? 對於那些我必須在pages_controller和list.html.erb中做的更改,以便我得到解決方案......
您可以通過以下方式訪問任何Controller中的任何模型
@instance_variable = ModelClass.all
在你的情況下,它應該像下面這樣
pages_controller.rb
def list
@subjects_list = Subject.all
end
應用程序/視圖/頁/ list.html.erb
<% for subject in @subjects_list %>
<!-- Your Code Here -->
<% end %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.