rails 3 associations error

江枫思渺然 提交于 2019-12-20 04:15:32

问题


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

回答1:


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



回答2:


It should be @page.build_author instead of @page.author.build. But the logic of this still doesn't look right to me.



来源:https://stackoverflow.com/questions/9245476/rails-3-associations-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!