在Heroku控制台上运行循环或多行代码

时间:2022-07-18 20:43:17

Heroku console doesn't run loops in my app (though they work on my local machine AND heroku permits individual actions to be taken).

Heroku控制台不会在我的应用程序中运行循环(虽然它们可以在我的本地计算机上运行而且heroku允许采取单独的操作)。

@companies.each do |c|
SyntaxError: /home/heroku_rack/lib/console.rb:150: syntax error, unexpected $end
@companies.each do |c|
                      ^
/home/heroku_rack/lib/console.rb:140:in `eval'
/home/heroku_rack/lib/console.rb:140:in `_eval'
/home/heroku_rack/lib/console.rb:73:in `block in process_command'
/usr/ruby1.9.2/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'

Do you know why these errors might be occurring?

你知道为什么会出现这些错误吗?

3 个解决方案

#1


28  

it's not an error - it's a feature of how Heroku let's you interact with your application via the heroku console command - whilst appearing like a full console it simply isn't. Each line is transmitted over http and evaluated when you press enter so you can't actually use multiline commands, this will work though;

这不是一个错误 - 它是Heroku让你通过heroku控制台命令与你的应用程序交互的一个特性 - 虽然看起来像一个完整的控制台,但它根本不是。每个行都通过http传输并在按Enter键时进行评估,因此您无法实际使用多行命令,但这样可以正常工作;

User.all.each {|user| user.update_attributes(:active => true) }

if it can't be written on one line you'll need to use a rake task or such like

如果它不能写在一行上,你需要使用rake任务或类似的东西

EDITED: To contain correct syntax

编辑:包含正确的语法

#2


4  

The proper syntax for this would be (at least for Ruby 1.9.2 on Heroku):

适当的语法是(至少对于Heroku上的Ruby 1.9.2):

User.all.each {|user| user.update_attributes(:active => true)}

Using John's method didn't work for me (and I ran into this issue a 2nd time so it was time to write an answer that worked for others).

使用John的方法对我不起作用(我第二次遇到这个问题所以是时候写一个适合其他人的答案)。

By the way, if you want multiple lines of code, you have to pass it all in as one line like this:

顺便说一下,如果你想要多行代码,你必须将它全部作为一行传递,如下所示:

User.all.each {|user| user.some_attribute = true; user.some_other_attribute = false; user.save }

#3


0  

the best way I found so far to execute multi line code of arbitrary length in Heroku console is to wrap my code into a class with the executable code wrapped into a class method

到目前为止,我发现在Heroku控制台中执行任意长度的多行代码的最好方法是将我的代码包装到一个类中,将可执行代码包装到类方法中

class DoSomething
  def self.run
    puts "line1"
    puts "line2 .... etc"
  end
end

then paste this into the console and hit Enter

然后将其粘贴到控制台并按Enter键

now your class is available to you and you can invoke the method you defined on it:

现在您的类可供您使用,您可以调用您在其上定义的方法:

DoSomething.run

DoSomething.run

so, this way there's no need to jump through the hoops and squeeze everything onto a single line

所以,这样就没有必要跳过篮球,把所有东西都挤到一条线上

#1


28  

it's not an error - it's a feature of how Heroku let's you interact with your application via the heroku console command - whilst appearing like a full console it simply isn't. Each line is transmitted over http and evaluated when you press enter so you can't actually use multiline commands, this will work though;

这不是一个错误 - 它是Heroku让你通过heroku控制台命令与你的应用程序交互的一个特性 - 虽然看起来像一个完整的控制台,但它根本不是。每个行都通过http传输并在按Enter键时进行评估,因此您无法实际使用多行命令,但这样可以正常工作;

User.all.each {|user| user.update_attributes(:active => true) }

if it can't be written on one line you'll need to use a rake task or such like

如果它不能写在一行上,你需要使用rake任务或类似的东西

EDITED: To contain correct syntax

编辑:包含正确的语法

#2


4  

The proper syntax for this would be (at least for Ruby 1.9.2 on Heroku):

适当的语法是(至少对于Heroku上的Ruby 1.9.2):

User.all.each {|user| user.update_attributes(:active => true)}

Using John's method didn't work for me (and I ran into this issue a 2nd time so it was time to write an answer that worked for others).

使用John的方法对我不起作用(我第二次遇到这个问题所以是时候写一个适合其他人的答案)。

By the way, if you want multiple lines of code, you have to pass it all in as one line like this:

顺便说一下,如果你想要多行代码,你必须将它全部作为一行传递,如下所示:

User.all.each {|user| user.some_attribute = true; user.some_other_attribute = false; user.save }

#3


0  

the best way I found so far to execute multi line code of arbitrary length in Heroku console is to wrap my code into a class with the executable code wrapped into a class method

到目前为止,我发现在Heroku控制台中执行任意长度的多行代码的最好方法是将我的代码包装到一个类中,将可执行代码包装到类方法中

class DoSomething
  def self.run
    puts "line1"
    puts "line2 .... etc"
  end
end

then paste this into the console and hit Enter

然后将其粘贴到控制台并按Enter键

now your class is available to you and you can invoke the method you defined on it:

现在您的类可供您使用,您可以调用您在其上定义的方法:

DoSomething.run

DoSomething.run

so, this way there's no need to jump through the hoops and squeeze everything onto a single line

所以,这样就没有必要跳过篮球,把所有东西都挤到一条线上