Search code examples
javascriptruby-on-railsbookmarklet

EasyMarklet Rails gem: undefined method `[]' for nil:NilClass


I'm trying to add a bare bones marklet to my rails app. I followed the instructions at the repository and followed the example for a bare bones marklet here. Here is the js for my marklet (pulled straight from the example):

(function(){

  var protocol = 'http://'
  var host = '<%= Rails.application.config.action_controller.default_url_options[:host] %>';
  var port = '<%= Rails.application.config.action_controller.default_url_options[:port] %>';
  port = port === '' ? '' : ':' + port;
  var full_host = protocol + host + port;
  document.location = full_host + "/pages/new?url=" + encodeURIComponent(document.location)

})();

And my link in the view:

<%= link_to 'Bookmarklet', easymarklet_js('my_sample_bookmarklet.js.erb') %>

I'm getting this error now:

undefined method `[]' for nil:NilClass
  (in /Users/myname/Rails/sample/app/assets/javascripts/my_sample_bookmarklet.js.erb)

That's coming from the second or third line of my JS right? How do I fix this? My javascript skills are not very good, so any help is appreciated. Thanks in advance!


Solution

  • It sounds like Rails.application.config.action_controller.default_url_options may be nil. Can you confirm this by checking on the console? If so you'll just need to set it in your environment files. The standard default in development is:

    MyApp::Application.configure do
      # development.rb
      config.action_controller.default_url_options = { host: 'localhost', port: 3000 }
    end