1

I'm trying to use rails nested form_for helper, but I am getting the following error:

BlogPage(#49859550) expected, got Array(#31117360)

Here are my model objects:

class Blog < ActiveRecord::Base
  # Table Configuration
  set_table_name "blog"

 # Model Configuration
 belongs_to :item
 has_many :blog_pages
 accepts_nested_attributes_for :blog_pages, :allow_destroy => true
end

class BlogPage < ActiveRecord::Base
  # Table Configuration
  set_table_name "blog_page"

  # Model Configuration
  belongs_to :blog
end

Here is the form I generated (left out unnecessary HTML):

<% form_for :blog, :url => { :action => :create } do |blog_form| %>  
    <%= blog_form.text_field :title, :style => "width: 400px" %>  
    <% blog_form.fields_for :blog_pages do |page_fields| %>
        <% @blog.blog_pages.each do |page| %>  
            <%= page_fields.text_area :content, :style => "width: 100%",
                :cols => "10", :rows => "20" %>
        <% end %>
    <% end %>
<% end %>

Here are the parameters that are sent to the controller:

{"commit"=>"Save", "blog"=>{"blog_pages"=>{"content"=>"This is the new blog entries contents."}, "title"=>"This is a new blog entry.", "complete"=>"1"}, "authenticity_token"=>"T1Pr1g9e2AjEMyjtMjLi/ocrDLXzlw6meWoLW5LvFzc="}

Here is the BlogsController with the create action that gets executed:

class BlogsController < ApplicationController
  def new
    @blog = Blog.new # This is the line where the error gets thrown.  
    # Set up a page for the new blog so the view is displayed properly.
    @blog.blog_pages[0] = BlogPage.new
    @blog.blog_pages[0].page_number = 1
    respond_to do |format|
      format.html # Goes to the new.html.erb view.
      format.xml { render :xml => @blog }
      format.js { render :layout => false}
    end
  end

  def create
    @blog = Blog.new(params[:blog])

    respond_to do |format|
      if @blog.save
        render :action => :show
      else
        flash[:notice] = "Error occurred while saving the blog entry."
        render :action => :new
      end
    end
  end
end

If anyone can help me with this I would greatly appreciate it. I'm still pretty new to ruby and the rails framework and couldn't solve the problem on my own by googling.

Thanks.

2
  • You should add the line number and comment in you code where the error occurs. You should also redirect if @blog.save succeeds. The way it is now, refreshing the browser might cause duplicate records to be inserted. require 'pp' and use 'raise @blog.pretty_inspect' to see what kind of array you've got. Commented Feb 2, 2010 at 3:22
  • Thanks. I added some of the suggestions you recommended. What does adding require 'pp' do? Commented Feb 2, 2010 at 22:30

2 Answers 2

2

Have you seen this?

http://media.pragprog.com/titles/fr_arr/multiple_models_one_form.pdf

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the link. I'm not sure if I should use this approach anymore though because it seems like it would just be easier to use the field_for not nested with the model and just add a bit of logic in the controller.
0

Change your form to this:

<% form_for :blog, :url => { :action => :create } do |blog_form| %>  
    <%= blog_form.text_field :title, :style => "width: 400px" %>  
    <% blog_form.fields_for :blog_pages do |page_fields| %>
            <%= page_fields.text_area :content, :style => "width: 100%",
                :cols => "10", :rows => "20" %>
    <% end %>
<% end %>

If you use fields_for it iterates over blog_pages automaticaly. However I'm not sure if this caused errors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.