Note that there are some explanatory texts on larger screens.

plurals
  1. POpartial form for nested models in rails 3
    text
    copied!<p>I have two models pages and author, here is the code of model pages:</p> <h1>edit -1</h1> <p>now my models are as follows:</p> <pre><code>class Page &lt; ActiveRecord::Base validates :title, :presence =&gt; true belongs_to :author end </code></pre> <p>author model:</p> <pre><code>class Author &lt; ActiveRecord::Base has_many :pages end </code></pre> <p>and my form is as follows:</p> <pre><code> &lt;%= form_for(@page) do |f| %&gt; &lt;% if @page.errors.any? %&gt; &lt;div id="error_explanation"&gt; &lt;h2&gt;&lt;%= pluralize(@page.errors.count, "error") %&gt; prohibited this post from being saved:&lt;/h2&gt; &lt;ul&gt; &lt;% @page.errors.full_messages.each do |msg| %&gt; &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;p&gt; &lt;%= f.label :title %&gt;&lt;br /&gt; &lt;%= f.text_field :title %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :body %&gt;&lt;br /&gt; &lt;%= f.text_area :body %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.fields_for :author do |fields| %&gt; &lt;%= f.label :author %&gt;&lt;br /&gt; &lt;%= fields.text_field :author %&gt; &lt;% end %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :email %&gt;&lt;br /&gt; &lt;%= f.text_field :email %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :reference %&gt;&lt;br /&gt; &lt;%= f.select(:reference,[['google',1],['yahoo',2],['MSN',3],['Ask',4]]) %&gt; &lt;/p&gt; &lt;%= f.submit "Submit" %&gt; &lt;% end %&gt; </code></pre> <p>and controller :</p> <pre><code>class PagesController &lt; 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 =&gt; "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 =&gt; "Your post has been updated!" else render "edit" end end def destroy @page = Page.find(params[:id]) @page.destroy redirect_to pages_path, :notice =&gt; "Your page has been deleted!" end end </code></pre> <p>Now when i am submitting the form it is giving me this error:</p> <pre><code>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"=&gt;"✓", "authenticity_token"=&gt;"hzBlXsdUjEDDCLp036R8bJBwep6BdATSvJPNwt0M8Dg=", "page"=&gt;{"title"=&gt;"", "body"=&gt;"", "author"=&gt;{"author"=&gt;""}, "email"=&gt;"", "reference"=&gt;"1"}, "commit"=&gt;"Submit"} Show session dump Show env dump Response Headers: None </code></pre> <p>and one more problem if I add <code>accepts_nested_attributes_for :author</code> to my Page model, then the field is not displayed at all. I really want to accomplish this.. any help?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload