Maverick
Maverick

Reputation: 2760

rails 3 associations error

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

Upvotes: 1

Views: 447

Answers (2)

ranendra
ranendra

Reputation: 2512

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

Upvotes: 1

maro
maro

Reputation: 1506

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

Upvotes: 0

Related Questions