草庐IT

do_redirect

全部标签

ruby-on-rails - 如何做 "redirect_to and return"完全退出或立即 redirect_to?

我的代码是这样的:defindex@monkeys=Monkey.where(owner:current_user)enddefnew@monkey=Monkey.newname:'Monkey1',alive:true,owner:current_userenddefcreate@monkey=Monkey.createmonkey_params#Otherfieldsandsavehereenddefedit@monkey=Monkey.find_by(id:params[:id],owner:current_user)check_on_monkey_first(@monkey)e

ruby - 使用 do...end block 时 map、select 等的不同行为

这个问题在这里已经有了答案:Rubyblockandunparenthesizedarguments(1个回答)关闭7年前。当我使用map、select和其他Enumerable时,我注意到以下行为差异方法。假设我们有如下哈希:h={a:1}下面的代码按预期打印select的输出。ph.select{|k,v|true}#=>{:a=>1}但是,下面的代码显示输出是一个Enumerator,即使已经提供了一个block。ph.selectdo|k,v|trueend#=>#1}:select>知道为什么会出现这种行为差异吗?我经常遇到这个问题,因为我在工作时一直使用inspectp,这

ruby - "n.times do" block 只返回 n 的值

根据thisquestion的答案判断,我预计@n.timesdo"hello"end返回与@n值相等的“hello”数量。但是,无论我如何修改代码,我的Rails控制台都只返回@n的值,仅此而已。我做错了什么?例如,如果我先设置@n=10,那么代码的结果就是10。 最佳答案 不,我看不出您是如何从这些答案中推断出.timesblock应该返回任何内容的。它所做的就是运行指定block指定次数,仅此而已。block的返回值被丢弃。如果您想将“hello”打印到标准输出N次或做一些其他工作,就可以。n.timesdoputs'hell

ruby-on-rails - Ruby 中的 Proc.new : when do I need to use it?

date_validator在其示例中有评论:UsingProc.newpreventsproductioncacheissues这是否意味着,在我的代码中的任何地方,我都使用与当前时间相关的方法(Time.now、1.day.since(Time.zone.now)等),我应该用Proc.new{}?我不完全理解这一点,因为更换了time_now=Time.now.utc与time_now=Proc.new{Time.now.utc}对我来说没有意义(返回了新类型的对象)。那么,问题是,我应该何时以及如何将Proc.new与时间相关的方法一起使用?这是否仍然适用于最新版本的Ruby(

ruby-on-rails - 正则表达式 : How do I grab a block of text using regex?(在 ruby​​ 中)

我正在使用ruby​​,我正试图找到一种在{start_grab_entries}和{end_grab_entries}之间抓取文本的方法,如下所示:{start_grab_entries}iwanttograbthetextthatyouseehereinthemiddle{end_grab_entries}像这样:$1=>"iwanttograbthetextthatyouseehereinthemiddle"到目前为止,我尝试将其作为正则表达式:\{start_grab_entries}(.|\n)*\{end_grab_entries}但是,使用$1,这给了我一个空白。您知道我

ruby - 为什么分配变量时 do/end block 的行为/返回不同?

puts[1,2,3].mapdo|x|x+1end.inspect对于ruby​​1.9.2这会返回ruby1.8.7:#1#2#3分配一个变量...x=[1,2,3].mapdo|x|x+1end.inspectputsx[2,3,4]mustacheblock按预期工作:puts[1,2,3].map{|x|x+1}.inspect[2,3,4] 最佳答案 puts[1,2,3].mapdo|x|x+1end.inspect解析为:puts([1,2,3].map)do|x|x+1end.inspect即map在没有block

Ruby 嵌套每个 do 循环

我试图通过让这个程序运行来理解嵌套循环。如果可能的话,我想使用“每次做”的循环方式。现在循环执行所有第一个循环,然后是第二个……等等……我想做的是,执行第一个循环1次,然后下降到第二个循环一次……等等。这是我的代码(粘贴在下面)期望的输出是这样的:index3.03.0+-------------------------------------------------------+0-23.4-23.41-2226.74-2226.742-1.93464e+07-1.93464e+07代码classLogisticsFunctionputs"Entertwonumbers,bothbe

ruby - 乘号:* do?是什么意思

特别是,使用注入(inject),以下脚本,puts(1..5).inject{|x,y|x*y}和puts(1..5).inject(:*),如我所料,两者都有输出120。但是,脚本print[[1,2],[3,4],[5,6]].map{|x,y|x*y}有输出[2,12,30]正如预期的那样,但是脚本print[[1,2],[3,4],[5,6]].map(:*)引发ArgumentError:confused_ruby_map_inject.rb:1:in`map':wrongnumberofarguments(1for0)(ArgumentError)fromconfused

ruby-on-rails - respond_with redirect with notice flash message 不起作用

我正在使用Rails3.0.7。在我的Controller中:defcreate@subscription=Subscription\.new_from_nested_attributes_parameters(params[:subscription])if@subscription.saveflash[:notice]='TheSubscriptionwassuccessfullycreated.'endrespond_with@subscriptionend在View中:尽管正确保存了对象,但不打印任何内容。您知道我应该如何解决这个问题吗? 最佳答案

ruby-on-rails - rails : Where do I put my API methods?

我是Rails的新手,在做一些简单的事情(例如创建API调用)时我有点不知所措。我在/reports设置了一个路由,它有这个Controller:classReportsController1})step1_result=step1.parseputs"Done!"putsstep1_resultrescueExcon::Errors::ServiceUnavailable=>eputs"Didn'twork"logger.warne.response.messageretryendend#Endrequest_reportrequest_reportend当我第一次加载/report