English 中文(简体)
用阿雷拉数据型号从网上表格转换成MongoMapper模型
原标题:Converting a String from a Web Form to MongoMapper Model with Array Datatype

我有一个MongoMapper模式,并试图将 com-de-limit-limit-limit-limit-str-str带转换成将储存的Array。

主要问题是,像<代码>tags = “第一、二、三”[“第一”、“第二”、“第三”]。 而是作为<代码>[“第一、二、三”]

There are some other strange things going on as well: 1) In preen_tags I have to include the unless tags.nil? after every line 2) in preen_tags, using the debugger tags returns nil

这里我的模式

class Template
  include MongoMapper::Document
  validate :validate_tags
  after_validation :preen_tags

  key :template_id,     ObjectId

  key :title,           String
  key :description,     String

  key :tags,            Array

  timestamps!

  def validate_tags
    errors.add_to_base "You Must Enter At Least 1 Tag." if tags.blank? 
  end


  def preen_tags
    #return if tags.nil?     #why doesn t this work??
    #only alphanumeric chars allowed, except hyphens and commas
    tags = tags[0] if tags.is_a?(Array)
    tags = tags.gsub(/[^0-9a-z-,]/i,   ) unless tags.nil?
    #convert spaces to hyphens
    tags = tags.gsub(/s/,  - ) unless tags.nil?
    tags = tags.split(",") unless tags.nil?
  end

end
最佳回答

在将Sting改为控制器内部的阿雷,然后才将其转至《模型》处理完毕。

问题回答

这是因为在Mongo,由于违约方是阿雷拉。 就像你定义的那样。 因此,你可以尝试<代码>tags.empty?,而不是tags.nil?

在最后一种情况下,由于你试图找到头几个tag子,tag子就变得无足轻重,但内部没有一人。 Just nil. 你们的标签变得零。





相关问题
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: ...

热门标签