我有一个带有一系列链接的ruby类.因为现在我可以保存Paper对象,即使该数组包含的链接不是有效的URL.我有一个方法贯穿数组并验证网址,如果网址无效,则返回false.但是当我尝试调用Paper.save时,我想收到一条错误消息.那可能吗?
class Paper
include MongoMapper::Document
key :links, Array
validates_presence_of :links
def validate_urls
reg = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
status = []
links.each do |link|
if link.match(reg)
status.push('true')
else
if "http://#{link}".match(reg)
status.push('true')
else
status.push('false')
end
end
end
if status.include?('false')
return false
else
return true
end
end
end
Run Code Online (Sandbox Code Playgroud) 我和Rails和mongodb有一个小问题.我有一个小应用程序,在开发模式下工作得很好.它也可以在生产模式下工作,但是当我进入rails控制台时,我看不到任何内容.
user-001@marcus:/var/www/webapp% rails c RAILS_ENV="production"
You did not specify how you would like Rails to report deprecation notices for your RAILS_ENV=production environment, please set config.active_support.deprecation to :log, :notify or :stderr at config/environments/RAILS_ENV=production.rb
Loading RAILS_ENV=production environment (Rails 3.2.1)
1.9.3p0 :001 > User.all
=> []
Run Code Online (Sandbox Code Playgroud)
我的config/initialize/mongo.rb看起来像这样
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "webapp-#{Rails.env}"
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
MongoMapper.connection.connect if forked
end
end
Run Code Online (Sandbox Code Playgroud)
但它看起来也是空的
$ mongo localhost:27017/mail1up-production
MongoDB shell version: 1.8.2
Fri Feb 17 18:26:00 *** warning: spider monkey build without utf8 …Run Code Online (Sandbox Code Playgroud) 使用以下Store和Service模型,使用MongoMapper进行管理:
class Store
include MongoMapper::Document
key :service_ids, Array, :typecast => 'ObjectId'
many :services, :in => :service_ids
end
class Service
include MongoMapper::Document
key :name, String
many :stores, :foreign_key => :service_ids
end
Run Code Online (Sandbox Code Playgroud)
我有这个表格,用Formtastic完成:
<%= semantic_form_for @store, :url => admin_store_path(@store), :method => :put do |form| %>
<%= form.input :service_ids, :label => "Select Store Services",
:as => :check_boxes,
:collection => Service.all %>
<% end -%>
Run Code Online (Sandbox Code Playgroud)
控制器使用Inherited Resources,编辑操作是隐式的.
使用已与之关联的服务编辑@store时,后者的复选框不会显示为已选中.
Formtastic的README警告它不正式支持MongoMapper,但它也说人们已经成功地使用了两者,我在网上看到了一些这样的例子.
我怀疑Inherited Resources也不支持它,我从Devise + Simple Form看到的,都来自同一作者,不支持MM.他们正努力在他们的宝石中使用ORM适配器,但尚未准备好AFAIK.
我已经遇到了问题,我正在重写更新操作以使其工作:
def update
store = Store.find(params[:id])
if …Run Code Online (Sandbox Code Playgroud) 鉴于以下类,我如何允许节点与其他节点(例如parent_node和child_nodes)具有多对多的关系?
class Node
include MongoMapper::Document
key :text, String
end
Run Code Online (Sandbox Code Playgroud)