section belongs_to :page
page has_many :sections
利用public#show来显示相应permalink的Page所包含的所有sections.
get 'show/:permalink', :to => 'public#show'
class PublicController < ApplicationController layout 'public' def index # intro text end def show @page = Page.where(:permalink => params[:permalink], :visible => true) puts "get permalink:", params[:permalink] puts "permalink",@page if @page.nil? redirect_to(action: 'index') else #display the page content using show .html.erb end end end
`irb(main):048:0> p4 = Page.where(permalink:"page4")
Page Load (0.4ms) SELECTpages
.* FROMpages
WHEREpages
.permalink
= 'page4'
=> #
`irb(main):057:0> p5 = Page.last
Page Load (0.4ms) SELECTpages
.* FROMpages
ORDER BYpages
.id
DESC LIMIT 1
=> #
`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 #
Extracted source (around line #5):
<% @page.sections.visible.sorted.each do |section| %>
<% case section.content_type %> <% when 'HTML' %>
`
This issue has been resolved, thank you everyone!
Add a .first
Can I see your migration code?