rails 3 中嵌套模型的部分形式 [英] partial form for nested models in rails 3
问题描述
我有两个模型页面和作者,这里是模型页面的代码:
编辑 -1
现在我的模型如下:
class Page 真的归属地:作者结尾
作者模型:
class Author
我的表格如下:
<%= form_for(@page) do |f|%><% if @page.errors.any?%><div id="error_explanation"><h2><%=pluralize(@page.errors.count, error") %>禁止保存此帖子:</h2><ul><% @page.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>
<%结束%><p><%= f.label :title%><br/><%= f.text_field :title %></p><p><%= f.label :body %><br/><%= f.text_area :body %></p><p><%= f.fields_for :author do |fields|%><%= f.label :author %><br/><%= fields.text_field :author %><%结束%></p><p><%= f.label :email %><br/><%= f.text_field :email %></p><p><%= f.label :reference%><br/><%= f.select(:reference,[['google',1],['yahoo',2],['MSN',3],['Ask',4]]) %></p><%= f.submit 提交";%><%结束%>
和控制器:
class PagesController <应用控制器定义索引@total = 页数@pages = Page.find(:all)结尾高清秀@page = Page.find(params[:id])结尾定义新@page = Page.new结尾定义创建@page = Page.new(params[:page])如果@page.saveredirect_to pages_path, :notice =>数据已保存!"别的呈现新"结尾结尾定义编辑@page = Page.find(params[:id])结尾定义更新@page = Page.find(params[:id])如果@page.update_attributes(params[:page])redirect_to pages_path, :notice =>您的帖子已更新!"别的渲染编辑"结尾结尾销毁@page = Page.find(params[:id])@page.destroyredirect_to pages_path, :notice =>您的页面已被删除!"结尾结尾
现在当我提交表单时,它给了我这个错误:
ActiveRecord::AssociationTypeMismatch in PagesController#create作者(#40328004) 预期,得到 ActiveSupport::HashWithIndifferentAccess(#32291496)Rails.root: C:/rorapp应用程序跟踪 |框架跟踪 |完整跟踪app/controllers/pages_controller.rb:19:in `new'app/controllers/pages_controller.rb:19:in `create'要求参数:{utf8"=>✓","authenticity_token>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>页面"=>{标题"=>",身体"=>",作者"=>{作者"=>"},电子邮件"=>",参考"=>1"},提交"=>提交"}显示会话转储显示环境转储回复标题:没有任何
还有一个问题,如果我将 accepts_nested_attributes_for :author
添加到我的页面模型中,那么该字段根本不会显示.我真的很想完成这个..有什么帮助吗?
显然这与关联不起作用.我很抱歉这个错误.您可以执行以下操作,但是您的页面控制器正在对作者起作用,这并不合适.不过,您可以创建一个作者控制器,并包含 fields_for :pages,以便同时创建作者和第一页.
class PagesController <应用控制器定义新@author = Author.new@page = @author.pages.new结尾定义创建@author = Author.create(params[:author])结尾结尾类作者 <ActiveRecord::Basehas_many :pagesaccepts_nested_attributes_for :pages结尾类页
I have two models pages and author, here is the code of model pages:
edit -1
now my models are as follows:
class Page < ActiveRecord::Base
validates :title, :presence => true
belongs_to :author
end
author model:
class Author < ActiveRecord::Base
has_many :pages
end
and my form is as follows:
<%= form_for(@page) do |f| %>
<% if @page.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@page.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @page.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.fields_for :author do |fields| %>
<%= f.label :author %><br />
<%= fields.text_field :author %>
<% end %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :reference %><br />
<%= f.select(:reference,[['google',1],['yahoo',2],['MSN',3],['Ask',4]]) %>
</p>
<%= f.submit "Submit" %>
<% end %>
and controller :
class PagesController < ApplicationController
def index
@total = Page.count
@pages = Page.find(:all)
end
def show
@page = Page.find(params[:id])
end
def new
@page = Page.new
end
def create
@page = Page.new(params[:page])
if @page.save
redirect_to pages_path, :notice => "The data has been saved!"
else
render "new"
end
end
def edit
@page = Page.find(params[:id])
end
def update
@page = Page.find(params[:id])
if @page.update_attributes(params[:page])
redirect_to pages_path, :notice => "Your post has been updated!"
else
render "edit"
end
end
def destroy
@page = Page.find(params[:id])
@page.destroy
redirect_to pages_path, :notice => "Your page has been deleted!"
end
end
Now when i am submitting the form it is giving me this error:
ActiveRecord::AssociationTypeMismatch in PagesController#create
Author(#40328004) expected, got ActiveSupport::HashWithIndifferentAccess(#32291496)
Rails.root: C:/rorapp
Application Trace | Framework Trace | Full Trace
app/controllers/pages_controller.rb:19:in `new'
app/controllers/pages_controller.rb:19:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"hzBlXsdUjEDDCLp036R8bJBwep6BdATSvJPNwt0M8Dg=",
"page"=>{"title"=>"",
"body"=>"",
"author"=>{"author"=>""},
"email"=>"",
"reference"=>"1"},
"commit"=>"Submit"}
Show session dump
Show env dump
Response
Headers:
None
and one more problem if I add accepts_nested_attributes_for :author
to my Page model, then the field is not displayed at all. I really want to accomplish this.. any help?
Apparently this doesn't work in reverse with the association. I'm sorry for the mistake. You could do the following, but then your pages controller is acting on the author, which isn't really appropriate. You could make an author controller though, and include fields_for :pages, to have the author and the first page created at the same time.
class PagesController < ApplicationController
def new
@author = Author.new
@page = @author.pages.new
end
def create
@author = Author.create(params[:author])
end
end
class Author < ActiveRecord::Base
has_many :pages
accepts_nested_attributes_for :pages
end
class Page < ActiveRecord::Base
belongs_to :author
end
<%= form_for(@author, :url => pages_url) do |f| %>
<%= f.text_field :author %>
<%= f.fields_for :pages do |fields| %>
<%= fields.text_area :body %>
<% end %>
<% end %>
这篇关于rails 3 中嵌套模型的部分形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!