rails 3 关联错误 [英] rails 3 associations error
问题描述
我有一个表格页面和一个表格作者.每个页面属于一个作者.还为表和模型创建了迁移.但是在表单中使用它时出现此错误:
页面中没有方法错误#new显示 C:/rorapp/app/views/pages/_form.html.erb 其中第 1 行提出:nil:NilClass 的未定义方法`build'提取的源代码(围绕第 1 行):1: <%= form_for(@page, @page.author.build) 做 |f|%>2: <% if @page.errors.any?%>3:<div id="error_explanation">4:<h2><%=pluralize(@page.errors.count,error")%>禁止保存此帖子:</h2>模板包含的痕迹:app/views/pages/new.html.erbRails.root: C:/rorapp应用程序跟踪 |框架跟踪 |完整跟踪app/views/pages/_form.html.erb:1:在`_app_views_pages__form_html_erb___998576952_54480300'app/views/pages/new.html.erb:2:在`_app_views_pages_new_html_erb__638997451_40207104'要求参数:没有任何显示会话转储显示环境转储回复标题:没有任何
这是我的作者模型文件:
class Author
和页面模型:
class Page 真的归属地:作者结尾
这是模型表单的片段:
<%= form_for(@page, @page.author.build) 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>
有什么想法可以继续吗?
谢谢
编辑 - 1
这是我的操作方法 new
:
def new@page = Page.new@page.build_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.label :author %><br/><%= f.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 提交";%><%结束%>
编辑 - 2
这是我的控制器代码:
class PagesController <应用控制器定义索引@total = 页数@pages = Page.find(:all)结尾高清秀@page = Page.find(params[:id])结尾定义新@page = Page.new@page.build_author结尾定义创建@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 =>您的页面已被删除!"结尾结尾
需要在pages controller的新action中添加*@page.build_author*.
def new@page = Page.new@page.build_authorresponse_to do |格式|format.html # new.html.erbformat.json { 渲染 json: @page }结尾结尾
I have a table pages and a table author. Each page belongs to one author. Created migrations for the tables and models as well. But getting this error while using it in form:
NoMethodError in Pages#new
Showing C:/rorapp/app/views/pages/_form.html.erb where line #1 raised:
undefined method `build' for nil:NilClass
Extracted source (around line #1):
1: <%= form_for(@page, @page.author.build) do |f| %>
2: <% if @page.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(@page.errors.count, "error") %> prohibited this post from being saved:</h2>
Trace of template inclusion: app/views/pages/new.html.erb
Rails.root: C:/rorapp
Application Trace | Framework Trace | Full Trace
app/views/pages/_form.html.erb:1:in `_app_views_pages__form_html_erb___998576952_54480300'
app/views/pages/new.html.erb:2:in `_app_views_pages_new_html_erb__638997451_40207104'
Request
Parameters:
None
Show session dump
Show env dump
Response
Headers:
None
Here is my model file for author:
class Author < ActiveRecord::Base
has_one :page
end
And page model:
class Page < ActiveRecord::Base
validates :title, :presence => true
belongs_to :author
end
And here is the snippet of model form :
<%= form_for(@page, @page.author.build) 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>
Any ideas how to go ahead with it?
thanks
EDIT - 1
Here is my action method called new
:
def new
@page = Page.new
@page.build_author
end
And here is the form it is rendering:
<%= 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.label :author %><br />
<%= f.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 "Submit" %>
<% end %>
EDIT - 2
Here is my controller code:
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
@page.build_author
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
you need to add *@page.build_author* in the new action of pages controller.
def new
@page = Page.new
@page.build_author
respond_to do |format|
format.html # new.html.erb
format.json { render json: @page }
end
end
这篇关于rails 3 关联错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!