使用rails应用程序,我想使用table_print gem,但我不允许在gemfile中安装新的gem。我看到有一个独立的版本,我应该可以直接在我的机器上安装,但我不知道如何让它工作。
当我致电tp Invoice.all
时,我收到以下错误。
NoMethodError:main:Object
的未定义方法`tp'
我尝试使用以下代码的变体创建.irbrc文件,但没有快乐。
# Outside rails
$ irb
> require 'table_print'
> tp array_of_objects, options
# Inside rails, the gem has already been required by your Gemfile so all you need to do is
$ rails c
> tp array_of_objects, options
有没有人让table_print的独立版本与rails c一起使用?
答案 0 :(得分:1)
将数组直接传递给tp参数:
➩ ➩ irb
2.0.0-p645 :002 > require 'table_print'
=> true
2.0.0-p645 :003 > tp [{id: 1, first_name: "Tim", last_name: "Thing", email: "test@example.com", dob: "1985-01-01" }, {id: 2, first_name: "Rob", last_name: "Roberts", email: "test@example.com", dob: "1985-01-01"}, {id: 3, first_name: "Nancy", last_name: "Name", email: "test@example.com", dob: "1985-01-01"}]
ID | FIRST_NAME | LAST_NAME | EMAIL | DOB
---|------------|-----------|------------------|-----------
1 | Tim | Thing | test@example.com | 1985-01-01
2 | Rob | Roberts | test@example.com | 1985-01-01
3 | Nancy | Name | test@example.com | 1985-01-01
=> 0.000762
2.0.0-p645 :004 >
或者将数组保存为变量:
➩ ➩ irb
2.0.0-p645 :002 > require 'table_print'
=> true
2.0.0-p645 :004 > object = [{id: 1, first_name: "Tim", last_name: "Thing", email: "test@example.com", dob: "1985-01-01" }, {id: 2, first_name: "Rob", last_name: "Roberts", email: "test@example.com", dob: "1985-01-01"}, {id: 3, first_name: "Nancy", last_name: "Name", email: "test@example.com", dob: "1985-01-01"}]
=> [{:id=>1, :first_name=>"Tim", :last_name=>"Thing", :email=>"test@example.com", :dob=>"1985-01-01"}, {:id=>2, :first_name=>"Rob", :last_name=>"Roberts", :email=>"test@example.com", :dob=>"1985-01-01"}, {:id=>3, :first_name=>"Nancy", :last_name=>"Name", :email=>"test@example.com", :dob=>"1985-01-01"}]
2.0.0-p645 :005 > tp object
ID | FIRST_NAME | LAST_NAME | EMAIL | DOB
---|------------|-----------|------------------|-----------
1 | Tim | Thing | test@example.com | 1985-01-01
2 | Rob | Roberts | test@example.com | 1985-01-01
3 | Nancy | Name | test@example.com | 1985-01-01
=> 0.001
如果没有错误的完整痕迹,这就是我能给予的所有帮助。希望它有所帮助!
- NoMethodError让我觉得table_print不喜欢你传递它的ActiveRecord :: Relation。也许尝试将Invoice.all.map(&:attributes)
传递给tp