草庐IT

add_months

全部标签

ruby-on-rails - 如何在 "day, month, year"时间表达式中使用 "1.VARIABLE.ago"这样的字符串变量?

我有一个参数,就像params[:time_range]一样。在我的Controller中,我想使用这个时间范围参数在我的查询中指定一个特定范围,如下所示:#params[:time_range]="week"time=params[:time_range]query=Article.where(created_at:1.#{time}.ago)当然它不能像现在这样工作。有没有办法将params[:time_range]字符串转换成月、日、周类型?我尝试使用to_sym但那个week不是符号。当我尝试用1.week找到它的类时,我得到了Fixnum。有谁知道解决这个问题的方法吗?

ruby-on-rails - rails 5.x : How can I add a route at runtime without overwriting original routes table?

假设我有一个Controller操作应该导致将新路由添加到路由表中:defmake_routevanity_url=params[:vanity_url]vanity_redirect=params[:vanity_redirect]returnrenderjson:{status:400}unlessvanity_url&&vanity_redirectRails.application.routes.drawdogetvanity_url,to:redirect(vanity_redirect)endrenderjson:{status::ok}end当我触发这个Action时,它

ruby-on-rails - rails : to_date with a month/day/year format

我有一个简单的问题。我有一个带有created_at的实例变量。我如何将其转换为Month,day,year~September,13,1987当我尝试=@example.created_at在我看来它给了我1987-09-13奇怪的是,当我在控制台中执行此方法时,我得到了Sun,13Sep1987如何将我的变量转换为月、日、年?为什么它在控制台中返回不同的东西? 最佳答案 您可以通过指定格式来更改日期的显示方式,例如@example.created_at.strftime("%B,%d,%Y")#=>"September,20,2

ruby-on-rails - Rails 元编程 : How to add instance methods at runtime?

我正在Rails中定义我自己的AR类,它将包括为用户字段0-9动态创建的实例方法。用户字段不直接存储在数据库中,它们将一起序列化,因为它们不经常使用。以下是执行此操作的最佳方法吗?替代方案?应该从哪里调用添加方法的启动代码?classInfo 最佳答案 一个不错的方法,尤其是当您可能有超过0..9个用户字段时,将使用method_missing:classInfoUSER_FIELD_METHOD=/^user_field_(\n+)$/defmethod_missing(method,*arg)returnsuperunlessm

ruby-on-rails - (Time.now.utc.to_date + 1.month + 15.days) != (Time.now.utc.to_date + 15.days + 1.month)

这怎么可能?Time.now.utc.to_date+1.month+15.days#=>Mon,01Dec2014Time.now.utc.to_date+15.days+1.month#=>Sun,30Nov2014有人看过吗?/编辑我想我问错了问题。那你们怎么解释呢?Time.now.utc.to_date+(15.days+1.month)#=>Mon,08Dec2014Time.now.utc.to_date+(1.month+15.days)#=>Tue,09Dec2014(15.days+1.month)#=>3888000(1.month+15.days)#=>3888

ruby-on-rails - ruby rails : add a new route

我是RoR的新手,所以这是一个新手问题:如果我有一个Controllerusers_controller.rb并且我添加了一个方法foo,它不应该创建这条路由吗?http://www.localhost:3000/users/foo因为当我这样做时,我得到了这个错误:Couldn'tfindUserwithid=foo我当然添加了一个Viewfoo.html.erb编辑:我将这段代码添加到routes.rb但我得到了同样的错误:resources:usersdoget"signup"end 最佳答案 这在rails3中不会自动运行。

ruby-on-rails - rails : How to add scope to pg_search with location search?

我有两个型号,Location和Basic,我使用的是pg_search和geocodergem,如何使用geocodernear方法将范围添加到我的pg_search_scope或者是否有其他方法可以这样做?Location模型有纬度和经度列。在我的Controller中我可以这样做:#thisallowsmetosearchneartheLocationmodel@business=Location.near(params[:loc],15)#thissearchesthroughthepg_search_scope@term=Basic.search(params[:q])#I'

ruby-on-rails - 如果你想在生产模式下使用它,如何解决 "You need to add gem ' 守护进程到你的 Gemfile 的错误?

我正在尝试正确使用Capistrano和RVM,以便将我的RubyonRails3.2.2应用程序部署到运行Ubuntu10.04LTS的远程机器上。我接缝解决了我之前与“Rvm-CapistranointegrationonLinuxUbuntu”相关的问题。但是,在部署时我收到以下与DelayedJobgem相关的错误:...*executing"cd/srv/www//releases/20120314135318&&bundleinstall"servers:[""][]executingcommand[]rvm_path=/usr/local/rvm/usr/local/rv

ruby-on-rails - 将 Add 方法添加到 ActiveRecord 数组

我有模型类别和产品。如果我使用category.products该项目被添加到数组中,记录被保存到数据库中。我尝试将以下“添加”方法添加到数组类中,虽然它确实将new_product添加到数组中,但不会将其保存到数据库中。这是为什么?classArraydefadd(item)self更新:collection_proxy.rb有以下方法:def所以下面的扩展有效:classActiveRecord::Relationdefadd(*records)proxy_association.concat(records)&&selfendend解决方案:为CollectionProxy添加一

ruby - 使用 Ruby 和 Savon 的 SOAP 客户端 : How to add EncodingType attribute in Nonce element

我需要创建一个ruby​​web服务客户端(使用Savon)来对需要Nonce中的EncodingType的web服务进行soap调用。所以正确的soap消息将具有这样的Nonce元素:......SomeHashValue......但是在我的Savon客户端中,我不知道如何在Nonce元素中添加该属性。我的代码在这里:......client=Savon.clientdowsdl.endpoint="http://webservicehost/TestWebService"wsdl.namespace="namespace"wsse.credentials"username","p