Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 批量分配属性在嵌套形式中失败。无声错误?_Ruby On Rails_Ruby On Rails 3_Model_Nested Attributes - Fatal编程技术网

Ruby on rails 批量分配属性在嵌套形式中失败。无声错误?

Ruby on rails 批量分配属性在嵌套形式中失败。无声错误?,ruby-on-rails,ruby-on-rails-3,model,nested-attributes,Ruby On Rails,Ruby On Rails 3,Model,Nested Attributes,我知道这里有几个问题。我不熟悉编程和rails,所以请耐心听我说。我的目标是收集n标记对象,并在我的显示和索引操作中显示它们 更新 感谢两个回答的人。每个建议都把我推向正确的方向。通过传入一个空数组来初始化标记对象,我可以让rake任务创建帖子。但是,仍然没有创建标记。进一步检查后,我发现以下SQL异常: irb(main):002:0> u.posts.build(title: "a new day", tags: "jump") WARNING: Can't mass-assign p

我知道这里有几个问题。我不熟悉编程和rails,所以请耐心听我说。我的目标是收集
n
标记对象,并在我的显示和索引操作中显示它们

更新 感谢两个回答的人。每个建议都把我推向正确的方向。通过传入一个空数组来初始化
标记
对象,我可以让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>
Post
Model

class Tag < ActiveRecord::Base

belongs_to :post

end
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
我的rake任务:

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
tag = Faker::Lorem.words(1) # create a tag
post = user.posts.create!(tags_attributes: [tags: tags])

如果在rails中的模型上将某些属性声明为
attr\u accessible
,那么所有其他属性都会自动设置为
attr\u protected
。在我看来,您的问题可能源于这样一个事实,即您正在尝试创建一篇文章并同时分配
标记
属性。尝试将
:标记添加到您的
Post
模型中的
attr\u accessible
属性列表中,并查看是否修复了它

如果在rails中的模型上将某些属性声明为
attr\u accessible
,那么所有其他属性都会自动设置为
attr\u protected
。在我看来,您的问题可能源于这样一个事实,即您正在尝试创建一篇文章并同时分配
标记
属性。尝试将
:标记添加到您的
Post
模型中的
attr\u accessible
属性列表中,并查看是否修复了它

在控制器上的#创建中,是否要调用
@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
在“在控制器上创建”对话框中,是否要调用
@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

我能解决这个问题。在rdocs之后,为了设置嵌套属性,需要将哈希数组传递给*\u属性。这将删除我上面描述的错误,并设置对象id
tags\u id

我所要做的就是去掉这条线:

tag = []
post = user.posts.create!(title: title, description: description, content: content, tags_attributes: tag)
并将其替换为我的rake任务中的以下内容:

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
tag = Faker::Lorem.words(1) # create a tag
post = user.posts.create!(tags_attributes: [tags: tags])
现在,当我从控制台执行类似于标记的操作时,我看到:

[#<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">,
[#,

我能够解决这个问题。在rdocs之后,为了设置嵌套属性,您将散列数组传递给*\u属性。这将删除我上面描述的错误,并设置对象id
标记\u id

我所要做的就是去掉这条线:

tag = []
post = user.posts.create!(title: title, description: description, content: content, tags_attributes: tag)
并将其替换为我的rake任务中的以下内容:

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
tag = Faker::Lorem.words(1) # create a tag
post = user.posts.create!(tags_attributes: [tags: tags])
现在,当我从控制台执行类似于标记的操作时,我看到:

[#<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">,
[#,


如果这很重要,我会感到惊讶。但是到目前为止,我在web上看到的许多其他问题和示例都在attr#u accessible调用之前进行了accepts nested attributes调用。也许在进行#{name}之前需要定义它_属性白名单?@agmcleod我按您建议的顺序打电话,收到了相同的异常情况。@agmcleod我在这方面遇到了一个建议重命名模型的解决方案。我想知道模型的名称标签是否不可接受?如果这很重要,我会感到惊讶。但是我在web上看到的许多其他问题和示例都是如此ar在attr#u accessible调用之前已经有了accepts nested attributes调用。可能需要在生成#{name}之前定义它_属性白名单?@agmcleod我按照您建议的顺序进行了调用,并收到了相同的异常。@agmcleod我遇到了一个建议重命名模型的解决方案。我想知道模型的名称标签是否不可接受?谢谢@Batkins。我做了更改,在控制台中收到了以下错误
rake abortd!Tag(#38643420)应为,get String(#18449760)
我不确定是否可以通过用户关系构建帖子,并通过帖子关系在一行中创建标签。例如,
@post=@user.posts.build(tags:Tag.new)
可能需要拆分为
@post=@user.posts.build
,然后是一个带有
@post.tags.build
的新行。这只是一个猜测。无论如何,我的答案似乎解决了您遇到的
无法批量分配受保护属性的问题,这是您在询问que时遇到的问题stion,那么你会接受我的回答是正确的吗?你可以把新的错误作为一个单独的问题提出。我同意。如果你不介意,我会为我发布的问题指定正确的解决方案。我想说的是,我为发布的原始问题提供了正确的解决方案。谢谢@Batkins。我做了更改,并在中收到了以下错误控制台
rake中止!标记(#38643420)应为,获取字符串(#18449760)
我不确定是否可以通过用户关系构建帖子,并通过帖子关系创建标记,所有这些都在一行中。如中,
@post=@user.posts.build(标记:Tag.new)
可能需要拆分为
@post=@user.posts.build
,然后是一个带有
@post.tags.build
的新行。这只是一个猜测。无论如何,我的答案似乎解决了您遇到的
无法批量分配受保护属性的问题,这是您在询问que时遇到的问题stion,那么你会接受我的回答是正确的吗?你可以把新的错误作为一个单独的问题提出。我同意。如果你不介意,我将为我发布的问题指定正确的解决方案。我要说的是,我为发布的原始问题提供了正确的解决方案。在我的代码库中是正确的,但在本文中不是。同样的错误也发生了。F遵循您的建议并从attr_accessible调用中删除
标记
,可以生成示例数据。谢谢。在我的控制台中检查
post
对象后,似乎标记对象没有保存。有什么想法吗?仍然为空。