简体   繁体   中英

What is causing this AssociationTypeMismatch error?

I'm creating basic message board where many comments belong to a post and a post belongs to only one topic. My issue is that I'm not sure how create a new Topic from the Post model's form. I'm receiving an error in my Post controller:

ActiveRecord::AssociationTypeMismatch in PostsController#create

Topic(#28978980) expected, got String(#16956760)

app/controllers/posts_controller.rb:27:in `new'
app/controllers/posts_controller.rb:27:in `create'

app/controllers/posts_controller.rb:27:

@post = Post.new(params[:post])

Here are my models:

topic.rb:

class Topic < ActiveRecord::Base
  has_many  :posts, :dependent => :destroy
  validates :name,  :presence => true,
                    :length => { :maximum => 32 }
  attr_accessible   :name  
end

post.rb:

class Post < ActiveRecord::Base
  belongs_to :topic,    :touch => true
  has_many   :comments, :dependent => :destroy
  attr_accessible :name, :title, :content, :topic
  accepts_nested_attributes_for   :topics, :reject_if => lambda { |a| a[:name].blank? }
end

comment.rb:

class Comment < ActiveRecord::Base
  attr_accessible        :name, :comment
  belongs_to :post, :touch => true
end

I have a form:

<%= simple_form_for @post do |f| %>
  <h1>Create a Post</h1>
  <%= f.input :name %>
  <%= f.input :title %>
  <%= f.input :content %>
  <%= f.input :topic %>  
  <%= f.button :submit, "Post" %>  
<% end %>

And it's controller action: (posts create)

def create
  @post = Post.new(params[:post]) # line 27
  respond_to do |format|
    if @post.save
      format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
    else
      format.html { render :action => "new" }
    end
  end
end

In all of the examples I find, tags belong to posts. What I'm looking for is different and probably easier. I want a post to belong to a single tag, a Topic . How can I create a Topic through the Post controller? Can someone point me in the right direction? Thank you very much for reading my question, I really appreciate it.

I'm using Rails 3.0.7 and Ruby 1.9.2. Oh and here's my schema just in case:

create_table "comments", :force => true do |t|
  t.string   "name"
  t.text     "content"
  t.integer  "post_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "posts", :force => true do |t|
  t.string   "name"
  t.string   "title"
  t.text     "content"
  t.integer  "topic_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "topics", :force => true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
end

Thanks again.

You should have:

accepts_nested_attributes_for :topic

on Post rather than the other way around.

@post = Post.new(params[:topic])在我的控制器中修复了该错误。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM