Ruby on rails 嵌套资源不会创建数据,在事务开始后立即回滚事务
我的问题是不会创建嵌套在帖子中的标签 我有一个贴子,里面嵌套了标签。像这样Ruby on rails 嵌套资源不会创建数据,在事务开始后立即回滚事务,ruby-on-rails,nested-attributes,Ruby On Rails,Nested Attributes,我的问题是不会创建嵌套在帖子中的标签 我有一个贴子,里面嵌套了标签。像这样 resources :posts do resources :tags, :only => [:new, :create, :destroy] end 在我的标签控制器里我有这个 class TagsController < ApplicationController before_filter :authenticate_user! def new @post = Post.find
resources :posts do
resources :tags, :only => [:new, :create, :destroy]
end
在我的标签控制器里我有这个
class TagsController < ApplicationController
before_filter :authenticate_user!
def new
@post = Post.find(params[:post_id])
@tag = Tag.new
end
def create
@post = Post.find(params[:post_id])
@tag = @post.tags.build(params[:tags])
if @tag.save
flash[:notice] = "Tag created"
redirect_to @tag.post
else
flash[:error] = "Could not add tag at this time"
redirect_to @tag.post
end
end
end
您的
TagsController的create()
操作中有输入错误:
@tag=@post.tags.build(params[:tags])
用于创建标记的哈希应该是params[:tag]
,而不是params[:tags]
标记控制器的create()
操作中存在键入错误:
@tag=@post.tags.build(params[:tags])
用于创建标记的散列应该是params[:tag]
,而不是params[:tags]
您可以通过在服务器控制台中使用p@tag.errors
或p@tag.errors.full\u消息来调试错误。现在我可以创建一个标记,但是tagable_type和tagable_id的字段没有被创建,即使我在表单字段中键入了两个值。请在那里提供模型,感谢我的创建操作中我有@tag=@post.tags.build(params[:tags]),应该在params中标记,而不是标记。您可以通过在服务器控制台中使用p@tag.errors
或p@tag.errors.full_消息打印来调试错误。现在我可以创建一个标记,但是tagable_type和tagable_id的字段没有被创建,即使我在表单字段中键入了两个值。请在那里提供模型,感谢我的创建操作中我有@tag=@post.tags.build(params[:tags]),应该在params中标记,而不是标记。叹息
<%= form_for [@post, @tag] do |f| %>
<% if @tag.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@tag.errors.count, "error") %> prohibited this tag from being saved:</h2>
<ul>
<% @tag.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.text_field :tagable_type, :placeholder => "Type" %>
</div>
<div class="field">
<%= f.text_field :tagable_id, :placeholder => "Id" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
class Tag < ActiveRecord::Base
attr_accessible :post_id, :user_id, :tagable_type, :tagable_id
validates :post_id, presence: true
belongs_to :tagable, :polymorphic => true
belongs_to :post
belongs_to :user
end
class Post < ActiveRecord::Base
attr_accessible :body, :link, :thumbnail, :title, :user_id, :youtube, :youtube_id, :website_name, :image_field
default_scope order: 'posts.active DESC'
has_many :tags, :dependent => :destroy
has_many :comments, :as => :commentable, :dependent => :destroy
belongs_to :user
end
Started POST "/posts/18/tags" for 127.0.0.1 at 2013-06-26 15:54:14 +0200
Processing by TagsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"gwpTs0Qqcrre4tH974RrfpaENGZKtSbkJx2U0H67AcM=", "tag"=>{"tagable_type"=>"Player", "tagable_id"=>"2"}, "commit"=>"Create Tag", "post_id"=>"18"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? ORDER BY posts.active DESC LIMIT 1 [["id", "18"]]
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "tags" ("created_at", "post_id", "tagable_id", "tagable_type", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Wed, 26 Jun 2013 13:54:14 UTC +00:00], ["post_id", 18], ["tagable_id", nil], ["tagable_type", nil], ["updated_at", Wed, 26 Jun 2013 13:54:14 UTC +00:00], ["user_id", 1]]
(6.9ms) commit transaction
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = 18 ORDER BY posts.active DESC LIMIT 1
Redirected to http://localhost:3000/posts/18
Completed 302 Found in 15ms (ActiveRecord: 7.9ms)