Heim > Fragen und Antworten > Hauptteil
section belongs_to :page
page has_many :sections
利用public#show来显示相应permalink的Page所包含的所有sections.
get 'show/:permalink', :to => 'public#show'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
`irb(main):048:0> p4 = Page.where(permalink:"page4")
Page Load (0.4ms) SELECT pages
.* FROM pages
WHERE pages
.permalink
= 'page4'
=> #<ActiveRecord::Relation [#<Page id: 43, subject_id: 28, name: "page4", permalink: "page4", position: 1, visible: true, created_at: "2016-05-27 06:43:27", updated_at: "2016-05-27 06:43:54">]>`
`irb(main):057:0> p5 = Page.last
Page Load (0.4ms) SELECT pages
.* FROM pages
ORDER BY pages
.id
DESC LIMIT 1
=> #<Page id: 43, subject_id: 28, name: "page4", permalink: "page4", position: 1, visible: true, created_at: "2016-05-27 06:43:27", updated_at: "2016-05-27 06:43:54">`
`irb(main):065:0* p4.class
=> Page::ActiveRecord_Relation
irb(main):066:0> p5.class
=> Page(id: integer, subject_id: integer, name: string, permalink: string, position: integer, visible: boolean, created_at: datetime, updated_at: datetime)`
`
undefined method `sections' for #<Page::ActiveRecord_Relation:0x007facb084bc50>
Extracted source (around line #5):
<h2><%= @page.name %></h2>
<% @page.sections.visible.sorted.each do |section| %>
<p class="section">
1 2 |
|
`
世界只因有你2017-04-24 16:02:44
1 |
|