Rails 3 has_one/has_many 问题
我正在编写一个应用程序,其中包含一个包含多个表和连接表等的数据库...我当前正在使用的两个(并且被难住了)是我的页面表和我的模板表。
现在一个页面只能包含一个模板,但一个模板可以包含多个页面。
页面模型:
class Page < ActiveRecord::Base
has_one :template
accepts_nested_attributes_for :template
end
模板模型:
class Template < ActiveRecord::Base
has_many :pages
end
当用户创建页面时,我希望他们能够选择布局,但由于某种原因,选择列表没有显示
用于显示的 HTML:
<%= 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 %>
最后一次选择不会出现的任何原因出现吗?
预先感谢您的所有帮助!
编辑:
要解决问题,我所要做的就是将模型逻辑放入控制器中,然后在视图中调用该对象,它起作用了
控制器:
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
视图:
<div class="field">
<%= page.label :template %>
<%= page.select("template_id", @templates.collect { |t| [t.content, t.id] }, :include_blank => 'None') %>
</div>
希望这对其他人有帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一页可以“属于”模板:
而不是:
我会使用一个简单的collection_select:
First page could "belongs to" template:
And instead of:
I would use a simple collection_select: