jjzjj

from_str

全部标签

ruby-on-rails - rails : Copying attributes from an object to another using the "attributes" method

让模型Quote具有属性[price,description]让模型Invoice有属性[price,description,priority]让invoice模型Invoice中的对象具有属性{price:10,description:'lamp',priority:10}invoice={price:10,description:'lamp',priority:10}假设我想将invoice属性复制到新的quote。quote=Quote.new(invoice.attributes)这会引发一个错误,即priority在模型Quote中不存在。如何将invoice属性复制到新的q

ruby-on-rails - Rails 时间奇数 : "x days from now"

当用户注册到我的网站之一进行免费试用时,我将他们的帐户到期时间设置为“14.days.from_now”。然后在主页上我显示他们还剩多少天,这是我得到的:(user.trial_expires-Time.now)/86400(因为一天有86400秒,即60*60*24)有趣的是,结果超过14,因此四舍五入为15。在控制台中进行更仔细的调查后,这种情况只会在未来两天发生(如果您明白我的意思)。例如>>Time.now=>FriOct2911:09:2601002010>>future_1_day=1.day.from_now=>Sat,30Oct201011:09:27BST01:00#

ruby-on-rails - rails : remove decimal from number_to_currency

我有一个float价格:number_to_currency(m.price,:locale=>'en_us')我得到:$39.00如何删除.00,我想得到:$39 最佳答案 您可以按照记录将精度设置为0here在RailsAPI文档中。number_to_currency(m.price,locale::en,precision:0)请注意,您的价格将进行四舍五入,从38.50美元到39.49美元的任何价格都将显示为39美元。编辑:将区域设置:en_us替换为:en,这可能会在更多应用中启用。

ruby-on-rails - rails : Render view from outside controller

我正在尝试使用View创建HTML字符串。我想从一个不是Controller的类中呈现它。如何在Controller外使用Rails渲染引擎?类似于ActionMailer的做法?谢谢! 最佳答案 Rails5和6以更方便的方式支持这一点,在幕后处理创建请求和诸如此类的事情:rendered_string=ApplicationController.render(template:'users/show',assigns:{user:@user})这会呈现app/views/users/show.html.erb并设置@user实例

ruby - Ruby 中的 str.each 不工作

我正在学习Ruby。我在http://ruby-doc.org/core/classes/String.html找到了方法String#each.当我尝试使用它时...irb(main):001:0>"hello\nworld".each{|s|ps}NoMethodError:undefinedmethod`each'for"hello\nworld":String...但我得到了NoMethodError。我使用的是Ruby1.9.1p253,所以我认为我使用的不是旧版本。怎么回事? 最佳答案 Ruby1.9在String类上不

ruby-on-rails - rescue_from ActionController::Rails 4 中的 RoutingError

我遇到了以下错误:ActionController::RoutingError(Noroutematches[GET]"/images/favicon.ico")我想为不存在的链接显示error404页面。我怎样才能做到这一点? 最佳答案 在application_controller.rb中添加以下内容:#Youwanttogetexceptionsindevelopment,butnotinproduction.unlessRails.application.config.consider_all_requests_localr

ruby-on-rails - rails : Remove element from array of hashes

我有以下数组:array=[{"email"=>"test@test.com","name"=>"Test"},{"email"=>"testA@test.com","name"=>"TestA"},{"name"=>"TestB","email"=>"testB@test.com"},{"email"=>"testC@test.com","name"=>"TestC"},{"name"=>"TestD","email"=>"testD@test.com"},{"email"=>"testE@test.com"},{"name"=>"TestF","email"=>"testF@tes

用于散列 : each element the key and derive value from it 的 Ruby 数组

我有一个字符串数组,想用它来哈希。数组的每个元素都是键,我想根据该键计算值。是否有Ruby方法可以做到这一点?例如:['a','b']转换为{'a'=>'A','b'=>'B'} 最佳答案 您可以:a=['a','b']Hash[a.map{|v|[v,v.upcase]}] 关于用于散列:eachelementthekeyandderivevaluefromit的Ruby数组,我们在StackOverflow上找到一个类似的问题: https://stack

ruby-on-rails - 3.days.ago, 2.hours.from_now 等没有 Rails?

有些书提到一些gem可以用#days,#megabytes,#minutes等装饰数字。这只在ActiveSupport中,还是是否有较小的gem提供此功能以用于(小型)非railsgem?我想在一个小小的gem中将此功能用作DSL的一部分。 最佳答案 我不确定除了ActiveSupport之外是否还有其他可用的gem,但是自己制作一个小版本真的很简单:classFixnumSECONDS_IN_DAY=24*60*60defdaysself*SECONDS_IN_DAYenddefagoTime.now-selfendend3.d

ruby-on-rails - rails : getting data from a table for each iteraction of a loop

我有一个循环(针对@dataset中的项目),我希望在每次迭代中从另一个表中获取不同的数据并进行一些将在View中打印的操作。我无法从循环中使用的数据集中获取此数据。如何根据MVC执行此操作?我可以将代码放入循环中,在View中,但我认为这太可怕了。我必须使用助手来执行此操作,并从View中调用该函数吗? 最佳答案 如果你有一个表,想从另一个表中获取数据,通常是在has_many关系的情况下。例如,我们有@people(Person模型),每个人都有has_many地址(Address模型)。在这些情况下,最好的办法就是这样做#Co