Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:38:44+00:00 2026-05-29T17:38:44+00:00

I have two models pages and author, here is the code of model pages:

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:38:45+00:00Added an answer on May 29, 2026 at 5:38 pm

    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 %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, Page and PageContent. class Page < ActiveRecord::Base has_many :page_contents end
so I have two models: class User < ActiveRecord::Base has_and_belongs_to_many :followed_courses, :class_name => Course
I have the following two models: class PhotoAlbum < ActiveRecord::Base belongs_to :project has_many :photos,
I have two models, Article and Post that both inherit from a base model
I currently have this chunk of model code: class Book(models.Model): title = models.CharField(max_length=100) num_pages
I have two models: Novel has_many :pages Page belongs_to :novel I want to list
I have two models for Publications and Employees: class Publication(models.Model): BOOK_CHAPTER = 1 ARTICLE
I have two models, Room and Image . Image is a generic model that
I have two models e.g. Book and Author . An author can have many
I have the following: class AccountAdmin(models.Model): account = models.ForeignKey(Account) is_master = models.BooleanField() name =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.