Search code examples
ruby-on-railsrubyslim-lang

Ruby runs on page load in Javascript in slim file


Basically I want to render this partial and build an object when someone clicks the button.

The problem I am having is that on page load the '@node.logical_ports.new' portion is actually being run, I don't want it to run until event has been triggered.

javascript:
  $(function(){
    $('body').on('click', '.add', function(){
      $(this).html("#{escape_javascript(render 'logical_port_form', logical_port: @node.logical_ports.new)}");
    });
  });

Solution

  • That's impossible, you cannot send Ruby down the wire to the client to be run at a later date. Ruby executes server-side, before the client has received anything. You need to rethink the way your code is architected.