English 中文(简体)
如何正确将默认条目添加到铁路上的Ruby 数据库?
原标题:How to properly add default entries to database in Ruby on Rails?

大部分我在种子.rb的条目 很简单,像这样:

User.create!(
name: "Peter"
admin: false;
# etc.
)

如果我得到“ T 质量指派保护属性” 错误, 我对模型稍作修改, 使用用户. rb :

attr_accessible: name, admin

至今为止还不错。但是我该如何在由铁路宝石生成的桌子上播种条目?它为我的应用程序添加了另一个引擎? Forem 。我肯定还有其他的。

我在我的种子.rb档案中添加了这些条目:

Forem::Category.create!(
name: "cat1"
)

Forem::Forum.create!(
title: "forum1",
description: "forum1 description",
category_id: 1
)

Forem::Topic.create!(
forum_id: 1,
user_id: 1,
subject:  topic1 ,
locked: false,
pinned: false,
hidden: false,
)

类别和论坛产生,主题不是:

Can t mass-assign protected attributes: forum_id, user_id, locked, pinned, hidden

如果我有一个主题.rb 模型, 我会知道该怎么做。 < 坚固> 但我没有它 。 Forem is a < a href="http://degegapi. rubyonrails.org/ classes/Rails/Engine.html" rel="nofollow">engine , 我不知道如何让模型主题的某些属性可见。rb。

我知道这行在申请中 rb:

config.active_record.whitelist_attributes = true

能够防止大规模任务。 禁用它会留下一个巨大的安全漏洞, 所以它不是一个选项。 禁用它也不允许我进入主题桌 。

我还尝试使用固定装置,我把这个添加到种子.rb的档案中:

require  active_record/fixtures 
Fixtures.create_fixtures("#{Rails.root}/test/fixtures", "topics.yml")

测试/外观.yml:

one:
  id: 1
  forum_id: 1
  user_id: 1
  subject: "topic1"
  created_at: 2012-05-19 19:54:19
  updated_at: 2012-05-19 19:54:20
  locked: false
  pinned: false
  hidden: false
  last_post_at: 2012-05-19 19:54:21
  state: "open"
  views_count: 3

我得到的错误是 - < 坚固 > 未初始化的固定固定定着

我的种子和固定有什么问题?还是我应该移民?

最佳回答

使它瘫痪留下一个巨大的安全漏洞,因此它不是一个选项。

不,它不是一个巨大的安全洞。 这是一个有争议的争论, 但 < code> attr_ accessible (和变量) (在我和其他许多人看来) 并不是解决阻止用户创建/ 更新对象/ 属性的问题的好办法。 换句话说, < code> attr_ accessible 是控制器问题的示范解决办法。 因为控制器的任务是确保数据被清理和使用, 检查当前用户是否被允许这样做, 等等 。

因此,我要做的是删除所有 atr_accessible 的引用,并将 whitelist_atripitte 设为假 。

然后由您在控制器中过滤您的参数。 您可以在 < a href=" https://gist.github.com/ 195644" rel=“ no follow” > this gist 或使用 < a href="https://github.com/rails/strong_paraters" rel=“ nofollow” > rails/strong_paraters 或任何其他可能令您高兴的方法中做 。

在那日之后,你们在播种的时候,将不再有这些问题,

问题回答

种子. rb 只是一个红宝石代码。 您不必在一行中创建全部资源。 尝试类似的方式

topic = Forem::Topic.create(
  :subject => "topic 1",
  :locked => false
  # etc
)

topic.user_id = 1
topic.save




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签