samedi 29 janvier 2022

ActiveRecord::UnknownAttributeError when adding passive column

I'm trying to run this migration, and the goal is to not have any server downtime while adding new columns. Since these columns have no relationships to other tables, exist in the code, or are required, I assumed this would be possible. But, after running the migration I observed UnknownAttributeError's being thrown when instantiating a new instance of the model.

The error:

Controller/existing_table/some_method::ActiveRecord::UnknownAttributeError
/code/app/models/existing_table.rb:40:in `new’

The line from the error:

e = ExistingTable.new(existing_table.attributes.except("id"))

The migration:

class AddTestFieldToExistingTable < ActiveRecord::Migration
  def change
    add_column :existing_table, :test_field, :integer
  end
end

When I go to recreate the issue by opening up a console and executing lines similar as above, I don't get the error. My guess is that there is some attributes cached and we do need to have downtime while running these "passive" migrations.

I wonder if the 'existing_table' is still in memory, doesn't have the 'test_field' attribute, and when creating a new instance of ExistingTable it doesn't know how to assign the field. Since I cannot recreate it without starting a new instance, I would be guessing on an alternative solution of re-writing the ExistingTable constructor.

Is there a way to avoid restarting the rails instance or re-writing this code? Am I on the right track?

mercredi 26 janvier 2022

Why are jekyll/bundler programs installing, but not appearing in gem list

I have "installed" bundler and jekyll without issue per the following trace:

$ gem install jekyll bundler   
Successfully installed unicode-display_width-1.8.0
Successfully installed terminal-table-2.0.0
Successfully installed safe_yaml-1.0.5
Successfully installed rouge-3.27.0
Successfully installed forwardable-extended-2.6.0
Successfully installed pathutil-0.16.2
Successfully installed mercenary-0.4.0
Successfully installed liquid-4.0.3
Successfully installed rexml-3.2.5
Successfully installed kramdown-2.3.1
Successfully installed kramdown-parser-gfm-1.1.0
Building native extensions. This could take a while...
Successfully installed ffi-1.15.5
Successfully installed rb-inotify-0.10.1
Successfully installed rb-fsevent-0.11.0
Successfully installed listen-3.7.1
Successfully installed jekyll-watch-2.2.1
Building native extensions. This could take a while...
Successfully installed sassc-2.4.0
Successfully installed jekyll-sass-converter-2.1.0
Successfully installed concurrent-ruby-1.1.9
Successfully installed i18n-1.9.0
Building native extensions. This could take a while...
Successfully installed http_parser.rb-0.8.0
Building native extensions. This could take a while...
Successfully installed eventmachine-1.2.7
Successfully installed em-websocket-0.5.3
Successfully installed colorator-1.1.0
Successfully installed public_suffix-4.0.6
Successfully installed addressable-2.8.0
Successfully installed jekyll-4.2.1
Successfully installed bundler-2.3.6
28 gems installed

but running bundle or jekyll returned a command not found error. Okay, no problem, I ran gem env to find the GEM PATH and added that to my path. Now bundle and jekyll are found, but they both give the following types of errors:

$ jekyll
/usr/lib/ruby/3.0.0/rubygems.rb:265:in `find_spec_for_exe': can't find gem jekyll (>= 0.a) with executable jekyll (Gem::GemNotFoundException)
        from /usr/lib/ruby/3.0.0/rubygems.rb:284:in `activate_bin_path'
        from /home/<user>/.local/share/gem/ruby/3.0.0/bin/jekyll:25:in `<main>'
$ bundle
/usr/lib/ruby/3.0.0/rubygems.rb:265:in `find_spec_for_exe': can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException)
        from /usr/lib/ruby/3.0.0/rubygems.rb:284:in `activate_bin_path'
        from /home/<user>/.local/share/gem/ruby/3.0.0/bin/bundle:25:in `<main>'

where I've edited the above snippet to remove identifying information. A few things:

  • gem update --system does not work. I get,
Updating rubygems-update
Successfully installed rubygems-update-3.3.6
ERROR:  While executing gem ... (Errno::ENOENT)
    No such file or directory @ dir_chdir - /usr/lib/ruby/gems/3.0.0/gems/rubygems-update-3.3.6
  • gem uninstall jekyll and gem uninstall bundler say that I do not even have these gems installed. They appear in the GEM_PATH folder that I added to my PATH, but running gem list --local do not show these programs at all.
  • I can seemingly install these jekyll and bundler infinitely as if they were not installed in the first place.

I have been working on this issue for a few days now on this forum and have not found a solution. Any pointers would be greatly appreciated. Thank you!

How do I make this form submit by hitting return instead of physically hitting the "+" button *question updated*

Ruby on rails: (really new to this)

Okay so I have this code for a form that I want to submit when I hit the return key.

I realized I have to add this code somewhere

reference: Rails 4: how to allow user to submit form with enter key, without submit button

$('idhere').keypress(function(e){
    if(e.which == 13){
         $(this).closest('form').submit();
     }
  });
<div class="input-group">
    <%= f.text_field :sample_name, class: 'form-control', hidden: true %>
    <%= f.collection_select :sample_name2, @sample, :name, :name, {include_blank: true}, {class: 'form-control selectpicker sa-sample-name', data: {'live-search' => 'true'}} %>
    <%= f.select :rank, 1..3, {}, {class: 'form-control selectpicker sa-rank', data: {'live-search' => 'true'}} %>
    <%= f.submit '   +   ', class: 'btn btn-outline-primary' %>
  </div>

So my first question is, do I add the id to f.select or f.submit? Only reason I ask is because in my head if the user is already clicked into f.select would they have to be able to click return from there?

And do I add the code to my application.js file or into the .erb file that I have the original code in? And if I have to add it to my application.js file is there a specific place that it needs to go?

also if I make my id:

id: 'sa_submit'

Would I just reference it as sa_submit or .sa_submit or :sa_submit or name-of-erb-file.sa_submit?

undefined method for #

I have User(has_one :profile, as: :profilable), Profile(has_many :privacies and accepts_nested_attributes_for :privacies) and Privacy(belongs_to :profile).

Routes:

get   "/u/edit/privacies",     action: :privacies,           as: :edit_user_privacies
patch "/u/edit/privacies",     action: :update_privacies,    as: :update_user_privacies

Controller:

def privacies
  respond_with @user
end

View:

<%= simple_nested_form_for @user.profile, as: :profile_privacies, url: update_user_privacies_path do |privacie_form| %>

        <div class="row">
            <div class="col-xs-12 col-md-6 text-muted text-semibold">
                Marque as opções de preferência para sua privacidade.
            </div>
        </div>

        <%= privacie_form.fields_for :privacies do |privacy| %>

            <div class="row">
                <div class="col-xs-12">
                    <h3 class="section-title">Fotos</h3>
                    <hr class="section-divider" style="margin-bottom:10px;"/>
                </div>
                <div class="col-xs-12">
                    <%= privacy.pictures as: :check_boxes, collection: Database::Privacy.kinds, label: false, wrapper: :default_checkbox_form %>
                </div>
            </div>

dimanche 23 janvier 2022

Permission Denied installing boring_generators in ruby on rails 6

I'd like to install boring_generators gem in my ruby on rails 6 project. But I couldn't install it, I got errors like below. I would be grateful if anyone gives me the idea to fix it. gem install boring_generators

ERROR: While executing gem ... (Errno::EACCES) Permission denied @ rb_file_s_symlink - (../acorn/bin/acorn, C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/boring_generators-0.11.0/tmp/templates/app_template/node_modules/.bin/acorn)

samedi 22 janvier 2022

In the destroy function of my Controller i want to delete current form id and previous n form id's

In my controller currently i'm deleting a letter from an author with id:x which is posted to the customer(state:delivered) in a database table which is an active record. I also have other states: written, posted, read. for the same author.

How do i delete all the states of the same author on pressing the delete button.

This is the old code for deleting single state:

  def destroy
    @letter = ::Adp::WritestheUpdate.find(params[:id])
    @letter.destroy
    respond_to do |format|
        format.html { redirect_to manage_adp_writes_the_updates_path, notice: 'Successfully deleted.' }
        format.json { head :no_content }
    end
  end

This is the new code i have tried for deleting multiple states

  def destroy
    @letters = author.ADP::WritestheUpdate.includes(:written, :posted, 
    :read,:deleted).find(params[:id])
    @letters.destroy
    respond_to do |format|
        format.html { redirect_to manage_adp_writes_the_updates_path, notice: 
         'Successfully deleted.' }
        format.json { head :no_content }
    end
  end

mercredi 19 janvier 2022

Getting uninitialized constant Moped even though moped has been totally removed (upgrading old App)

Upgrading a very old Application from Rails 3.22 to 4.2.11.3. (I know it is old and terribly outdated).

I cannot figure out this error I am getting.

uninitialized constant Moped

I understand the uninitialized constant error itself. However, There is no reference to Moped in the application or any included Gem. The app does us MongoDB, however Rails 4 moves to the Mongo Ruby driver which I am aware of, and have previously made the change over in another upgrade that had the same Rails versions.

I have opened every Gem we include and searched the directory for Moped, Moped:: and moped and there is nothing.

I followed the stacktrace but was unable to find any strings to pull on.

activesupport (4.2.11.3) lib/active_support/inflector/methods.rb:261:in `const_get'
activesupport (4.2.11.3) lib/active_support/inflector/methods.rb:261:in `block in constantize'
activesupport (4.2.11.3) lib/active_support/inflector/methods.rb:259:in `each'
activesupport (4.2.11.3) lib/active_support/inflector/methods.rb:259:in `inject'
activesupport (4.2.11.3) lib/active_support/inflector/methods.rb:259:in `constantize'
activesupport (4.2.11.3) lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
activesupport (4.2.11.3) lib/active_support/core_ext/marshal.rb:10:in `rescue in load_with_autoloading'
activesupport (4.2.11.3) lib/active_support/core_ext/marshal.rb:5:in `load_with_autoloading'
actionpack (4.2.11.3) lib/action_dispatch/middleware/cookies.rb:424:in `deserialize'
actionpack (4.2.11.3) lib/action_dispatch/middleware/cookies.rb:459:in `[]'
devise (4.8.1) lib/devise/strategies/rememberable.rb:60:in `remember_cookie'
devise (4.8.1) lib/devise/strategies/rememberable.rb:15:in `valid?'
warden (1.2.7) lib/warden/proxy.rb:356:in `block in _run_strategies_for'
warden (1.2.7) lib/warden/proxy.rb:354:in `each'
warden (1.2.7) lib/warden/proxy.rb:354:in `_run_strategies_for'
warden (1.2.7) lib/warden/proxy.rb:324:in `_perform_authentication'
warden (1.2.7) lib/warden/proxy.rb:105:in `authenticate'
devise (4.8.1) lib/devise/controllers/helpers.rb:126:in `current_user'
devise (4.8.1) lib/devise/controllers/helpers.rb:122:in `user_signed_in?'
activesupport (4.2.11.3) lib/active_support/callbacks.rb:432:in `block in make_lambda'
activesupport (4.2.11.3) lib/active_support/callbacks.rb:144:in `block (2 levels) in halting_and_conditional'
activesupport (4.2.11.3) lib/active_support/callbacks.rb:144:in `each'
activesupport (4.2.11.3) lib/active_support/callbacks.rb:144:in `all?'
activesupport (4.2.11.3) lib/active_support/callbacks.rb:144:in `block in halting_and_conditional'
activesupport (4.2.11.3) lib/active_support/callbacks.rb:504:in `block in call'
activesupport (4.2.11.3) lib/active_support/callbacks.rb:504:in `each'
activesupport (4.2.11.3) lib/active_support/callbacks.rb:504:in `call'
activesupport (4.2.11.3) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.11.3) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.11.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.11.3) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.11.3) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.11.3) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.11.3) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.11.3) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.11.3) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.11.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.11.3) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
actionpack (4.2.11.3) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.11.3) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.11.3) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.11.3) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.11.3) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.11.3) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
actionpack (4.2.11.3) lib/action_dispatch/routing/route_set.rb:43:in `serve'
actionpack (4.2.11.3) lib/action_dispatch/routing/mapper.rb:49:in `serve'
actionpack (4.2.11.3) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.11.3) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.11.3) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.11.3) lib/action_dispatch/routing/route_set.rb:817:in `call'
omniauth (1.4.3) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.4.3) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.4.3) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.4.3) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.4.3) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.4.3) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.4.3) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.4.3) lib/omniauth/strategy.rb:164:in `call'
warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
rack (1.6.13) lib/rack/etag.rb:24:in `call'
rack (1.6.13) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.13) lib/rack/head.rb:13:in `call'
actionpack (4.2.11.3) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.11.3) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.13) lib/rack/session/abstract/id.rb:252:in `context'
rack (1.6.13) lib/rack/session/abstract/id.rb:247:in `call'
actionpack (4.2.11.3) lib/action_dispatch/middleware/cookies.rb:560:in `call'
actionpack (4.2.11.3) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.11.3) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.11.3) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.11.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.11.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.11.3) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.11.3) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
bugsnag (6.24.1) lib/bugsnag/integrations/rack.rb:51:in `call'
actionpack (4.2.11.3) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.11.3) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.11.3) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.11.3) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.11.3) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.11.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.11.3) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.11.3) lib/rails/rack/logger.rb:20:in `call'
rack-timeout (0.6.0) lib/rack/timeout/core.rb:151:in `block in call'
rack-timeout (0.6.0) lib/rack/timeout/support/timeout.rb:19:in `timeout'
rack-timeout (0.6.0) lib/rack/timeout/core.rb:150:in `call'
request_store (1.5.1) lib/request_store/middleware.rb:19:in `call'
actionpack (4.2.11.3) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.13) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.13) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.11.3) 
lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.13) lib/rack/lock.rb:17:in `call'
actionpack (4.2.11.3) lib/action_dispatch/middleware/static.rb:120:in `call'
rack (1.6.13) lib/rack/sendfile.rb:113:in `call'
railties (4.2.11.3) lib/rails/engine.rb:518:in `call'
railties (4.2.11.3) lib/rails/application.rb:165:in `call'
railties (4.2.11.3) lib/rails/railtie.rb:194:in `public_send'
railties (4.2.11.3) lib/rails/railtie.rb:194:in `method_missing'
puma (5.5.2) lib/puma/configuration.rb:249:in `call'
puma (5.5.2) lib/puma/request.rb:77:in `block in handle_request'
puma (5.5.2) lib/puma/thread_pool.rb:340:in `with_force_shutdown'
puma (5.5.2) lib/puma/request.rb:76:in `handle_request'
puma (5.5.2) lib/puma/server.rb:447:in `process_client'
puma (5.5.2) lib/puma/thread_pool.rb:147:in `block in spawn_thread'

I am just totally stumped as to where this reference to Moped is coming from. Tailing the puma log shows that the app is now booting but same error.

I'm kind of hoping someone maybe sees something I am missing or can provide any insights into previous experience that might help me eliminate this issue.

lundi 17 janvier 2022

Rails 3 initializing custom Railtie

In a Rails 3 project, I'm creating a custom Railtie in order to monkey patch the Rails migration generator. I'm using the multiverse gem as a source of inspiration as it's very close to what I'm trying to do, only in a higher version of Rails.

Here is my Railtie:

require "rails/railtie"


module MultiDb
  class Railtie < Rails::Railtie
    config.after_initialize do
      puts "Hello!"
    end

    generators do
      ActiveSupport.on_load(:active_record) do
        require "rails/generators/migration"
        Rails::Generators::Migration.prepend(MultiDb::MigrationTemplate)
      end
    end
  end
end

module MultiDb
  module MigrationTemplate
    def migration_template(source, destination, config = {})
      p "migration template"

      super
    end
  end
end

I'm just not sure how I'm supposed to actually load/register (not sure of the terminology here) when my application boots. I've tried adding an initializer that requires this file, but that hasn't done anything at all.

Rails3 getting 'Could not dump table "table_name" because of following StandardError' error in the schema.rb file for the table

enter image description here

#Could not dump table "table_name" because of following StandardError #Unknown type 'json' for column 'column_name'

mercredi 12 janvier 2022

NoMethodError (undefined method `category_id' for #

I am setting up a website for categories and topics and messages with votes from users for each category, topic, and message. Here I am trying to create a new vote object, and the error is NoMethodError (undefined method 'category_id' for #ActiveRecord::Relation[]), for vote. I find this strange because most errors with ActiveRecord::Relation objects involve Vote.where or Vote.find_by, whereas here I am just trying to reference a new or created object, vote.

Here is the code in my controller, and I will add more code if needed.

    def create
@vote = Vote.new(vote_params)
@vote.user_id = current_user.id
category = @category
topic = @topic
message = @message
  if category != nil && topic === nil && message === nil then
     @vote.category_id = category.id
     if @vote === Vote.find_by(user_id: current_user.id, category_id: category.id) then
  flash[:notice] = "You have already voted."
     elsif @vote != Vote.find_by(user_id: current_user.id, category_id: category.id) then
       @vote.save!
     flash[:notice] = "Thankyou for voting."
     redirect_to categories_path(category_id: category.id)
     end
  elsif category != nil && topic != nil && message === nil then
     @vote.category_id = category.id
     @vote.topic_id = topic.id
     if @vote === Vote.find_by(user_id: current_user.id, category_id: category.id, topic_id: topic.id) then
       flash[:notice] = "You have already voted."
     elsif @vote != Vote.find_by(user_id: current_user.id, category_id: category.id, topic_id: topic.id) then
       @vote.save!
       flash[:notice] = "Thank you for voting."
       redirect_to category_topics_path(category_id: category.id, topic_id: topic.id)
     end

  elsif message != nil then
      @vote.category_id = message.category_id
      @vote.topic_id = message.topic_id
      @vote.message_id = message.id
      if @vote === Vote.find_by(user_id: current_user.id, category_id: category.id, topic_id: topic.id, message_id: message.id)
      flash[:notice]= "You have already voted."
      elsif @vote != Vote.find_by(user_id: current_user.id, category_id: category.id, topic_id: topic.id, message_id: message.id)
        @vote.save!
      flash[:notice] = "Thank you for voting."
      redirect_to category_topic_messages_path(category_id: category.id, topic_id: topic.id, message_id: message.id)
     end
  end
end

Is there any way to allow generate_otp_backup_codes using validate_and_consume_otp?

I am using device-two-factor gem for otp generation in rails. I am using generate_otp_backup_code to a specific user for testing purpose which is generating otp successfully. validate_and_consume_otp allows otp created via generate_otp_secret whereas validate_and_consume_otp dosen't allow the backup code otp to login.

Is there any way to allow generate_otp_backup_codes via validate_and_consume_otp?

tinfoil /devise-two-factor
gem 'devise-two-factor'

In user.rb

devise  :two_factor_authenticatable,
otp_secret_encryption_key:Rails.application.secrets.secret_key_base
devise :two_factor_backupable, otp_backup_code_length:     6,
            otp_number_of_backup_codes: 10

To generate backup codes:

codes = current_user.generate_otp_backup_codes!
current_user.save!

jeudi 6 janvier 2022

Graphql-ruby Bulding Mutation/query Without the need of user authentication

Hey I have a graphql mutation which needs to be implemented before user logs in. Till now I have been using graphql endpoints only after User is fully authenticated. Since graphql controller inherits application controller which implements a before_action :authenticate_user! callback I always need a valid user inorder to use the graphql endpoints. Is there a way to configure certain graphql endpoint to not have a valid user.

How should I go about it?

mercredi 5 janvier 2022

Convert rails 3 list of routes to rails 6 list of routes

Having trouble converting my list of routes in rails 3 to a list of routes in rails 6.

This is an example of my list.

member_routes = {
    :validate_update_action                => :put,
    :rebuild_update_form                   => :put,
    :create_change_request                 => :post,
    :validate_create_change_request_action => :post,
    :destroy                               => :get,
    :apply_to_beta                         => :put,
    :update_history                        => :get
  }

I think the body should be something similar to what i have below but not sure what the exact syntax is.

{
    put 'validate_update_action';
    put 'rebuild_update_form';
    post 'create_change_request';
    post 'validate_create_change_request_action';
    get 'destroy';
    put 'apply_to_beta';
    get 'update_history';
} 

mardi 4 janvier 2022

Ruby ERB render and def function

I got an strange issue with my experiments with ERB. Here what code do I have:

# cat ./services_lbs2.erb
<%= def renderVip(description, template)
puts "zxc dfg"
end
%>
# locally and remote
Printing: <%= renderVip('123','456') %>

And here what I am getting in the irb:

irb(main):034:0> @template=File.read('./services_lbs2.erb')
=> "<%= def renderVip(description, template)\nputs \"zxc dfg\"\nend\n%>\n# locally and remotely monitored (all externals)\nPrinting: <%= renderVip('123','456') %>\n"
irb(main):035:0> zxc = ERB.new(@template,nil, "-")
=> #<ERB:0x00000000068d4d88 @safe_level=nil, @src="#coding:US-ASCII\n_erbout = String.new; _erbout.concat(( def renderVip(description, template)\nputs \"zxc dfg\"\nend\n).to_s); _erbout.concat \"\\n# locally and remotely monitored (all externals)\\nPrinting: \"\n\n; _erbout.concat(( renderVip('123','456') ).to_s); _erbout.concat \"\\n\"\n; _erbout.force_encoding(__ENCODING__)", @encoding=#<Encoding:US-ASCII>, @frozen_string=nil, @filename=nil, @lineno=0>
irb(main):036:0> zxc.result(binding)
zxc dfg
=> "renderVip\n# locally and remotely monitored (all externals)\nPrinting: \n"

I could not get the output like:

# locally and remotely monitored (all externals)\nPrinting: zxc dfg\n

Why is it so and how it can be fixed?

Thanks!