I am writing an application that contains a database with several tables and joining tables and so forth... the two I am working with currently (and am stumped on) are my pages table and my templates table.
Now a page can contain only one template, but a template can have many pages.
Model for Page:
class Page < ActiveRecord::Base
has_one :template
accepts_nested_attributes_for :template
end
Model for Template:
class Template < ActiveRecord::Base
has_many :pages
end
When a user creates a page, I want them to be able to select a layout, but for some reason the select list is not showing up
HTML for show:
<%= form_for(@page) do |page| %>
<% if @page.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@page.errors.count, "error") %> prohibited this page from being saved:</h2>
<ul>
<% @page.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= page.label "Page title" %><br />
<%= page.text_field :slug %>
</div>
<div class="field">
<%= page.label :active %>?<br />
<%= page.check_box :active %>
</div>
<%= page.fields_for :category do |cat| %>
<%= cat.label :category %>
<%= select :page, :category_id, Category.find(:all).collect{|c| [c.name, c.id] } %>
<% end %>
<%= page.fields_for :template do |temp| %>
<%= temp.label :template %>
<%= select :page, :template_id, Template.find(:all).collect{|t| [t.content, t.id] } %>
<% end %>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
Any reasons why the last select wouldn't show up?
Thanks in advance for all the help!
Edit:
All I had to do to fix the problem was put the Model logic in my controller and then call that object in the view and it worked
Controller:
def new
@page = Page.new
@categories = Category.find(:all)
@templates = Template.find(:all)
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @page }
end
end
View:
<div class="field">
<%= page.label :template %>
<%= page.select("template_id", @templates.collect { |t| [t.content, t.id] }, :include_blank => 'None') %>
</div>
Hope this helps someone else!