我知道cover this already这里有几个问题。我是编程和rails的新手,所以请耐心等待。我的目标是收集n
标记对象并在我的show和index操作中显示它们。
更新
感谢两位回答的人。每个建议都把我推向了正确的方向。我可以通过传入一个空数组来初始化tags
对象来获取rake任务来创建帖子。但是仍然没有创建标签。在进一步检查时,我得到以下SQL异常:
irb(main):002:0> u.posts.build(title: "a new day", tags: "jump")
WARNING: Can't mass-assign protected attributes: tags
(1.7ms) SELECT 1 FROM "posts" WHERE "posts"."title" = 'a new day' LIMIT 1
(0.5ms) COMMIT
=> #<Post id: nil, title: "a new day", description: nil, content: nil, user_id: 1, created_at: nil, updated_at: nil>
我的设置如下:
Tag
模型
class Tag < ActiveRecord::Base
belongs_to :post
end
Post
模型
class Post < ActiveRecord::Base
has_many :tags, autosave: true
attr_accessible :title, :description, :content, :tags_attributes
accepts_nested_attributes_for :tags, allow_destroy: true, reject_if: lambda {|attrs| attrs.all? {|key, value| value.blank?}}
#add n number of form fields to capture tags in each article.
def with_blank_tags(n = 3)
n.times do
tags.build
end
self
end
end
'查看'代码
<%= form_for(@post.with_blank_tags) do |f| %>
<div class="field">
<%= f.fields_for(:tags) do |tags| %>
<%= unless tags.object.new_record? tags.check_box('_destroy') + tags.label('_destroy', 'Remove Tag') end%>
<%= tags.label :tags, "Add a Tag"%>
<%= tags.text_field :tags %>
<%end%>
</div>
<%end%>
'控制器'代码
def new
@post = @user.posts.build
end
def create
@post = @user.posts.build(params[:post])
if @post.save?
respond_to do |format|
format.html { redirect_to @post, notice: 'Post was successfully created.' }
else
format.html { render action: :new }
end
end
end
我的佣金任务:
namespace :db do
desc "Fill database with sample data"
task :posts => :environment do
Rake::Task['db:reset'].invoke
make_users
make_posts
end
end
def make_users
puts "making users..."
5.times do |n|
name = Faker::Name.name
password = "foo"
email = "example-#{n+1}@example.com"
@user=User.create!(
codename: name,
email: email,
password: password,
password_confirmation: password)
end
end
def make_posts
puts "making posts..."
User.all(:limit => 3).each do |user|
10.times do
content = Faker::Lorem.paragraphs(3)
description = Faker::Lorem.words(10)
title = Faker::Lorem.words(4)
tag = []
post = user.posts.create!(title: title, description: description, content: content, tags_attributes: tag)
end
end
end
答案 0 :(得分:1)
如果在rails中的模型上声明某些属性为attr_accessible
,则所有其他属性将自动设置为attr_protected
。在我看来,您的问题可能源于您尝试创建帖子并同时分配tags
属性这一事实。尝试将:tags
添加到attr_accessible
模型中的Post
属性列表中,看看是否可以修复它。
答案 1 :(得分:0)
在你控制器的#create中,你不想打电话给@post.save
吗?您也不需要第二个.tags方法。只是一个简单的:
def create
@post = @user.posts.build(params[:post])
if @post.save
redirect_to @post, notice: 'Post was successfully created.' }
else
render action: :new
end
end
答案 2 :(得分:0)
我能够解决问题。在rdocs here之后,为了设置嵌套属性,您将哈希数组传递给* _attributes。这将删除我上面描述的错误并设置对象标识tags_id
。
我所要做的就是删除这一行:
tag = []
post = user.posts.create!(title: title, description: description, content: content, tags_attributes: tag)
并将其替换为我的rake任务内部:
tag = Faker::Lorem.words(1) # create a tag
post = user.posts.create!(tags_attributes: [tags: tags])
现在,当我从控制台执行类似Tag.all
的操作时,我看到:
[#<Tag id: 1, post_id: 1, tags: "---\n- adipisci\n", created_at: "2012-01-12 06:31:13", updated_at: "2012-01-12 06:31:13">,