当我使用<%= home_index_path %>在application.js.erb,为什么

Undefined local variable or method when I use <%= home_index_path %> in application.js.erb, why?

本文关键字:application js 为什么 erb path home index      更新时间:2023-09-26

如标题所述,我有一个application.js.erb文件,如下所示:

//= require jquery
//= require jquery-ui
//= require bootstrap
//= require html5shiv
//= require_tree .
$(document).ready(function(){
  $("#opening-first").fadeIn(1000, function() {
    $("#opening-second").fadeIn(1000, function() {
        $("#opening-container").delay(500).fadeOut(1000, function() {
            $("#body-overlay").fadeOut(1000);
        });
    });
  });
  $(".me").on('click', function(){
    fadeAndReload('<%= me_home_index_path %>');
  });
  $(".home").on('click', function(){
    fadeAndReload('<%= root_path %>');
  });
  function fadeAndReload(fileName) {
    var body = $("#reload-me");
    body.fadeOut(500, function() {
        body.load(fileName, function() {
        body.fadeIn(500);
      });
    });
  }
});

我的路由列表如下:

   home_index GET /home/index(.:format) home#index
me_home_index GET /home/me(.:format)    home#me
         root     /                     home#index

我得到以下错误:

undefined local variable or method `me_home_index_path' for #<#<Class:0x007f99d63b5728>:0x007f99d65ef9d0>
  (in /Users/[filepathhere]/javascripts/application.js.erb)

有什么问题吗?

为了它的价值,我不得不将这一行添加到我的application.js.erb文件的顶部,在我的require语句下,现在一切都正常工作了:

<% environment.context_class.instance_eval { include Rails.application.routes.url_
helpers } %>

me_home_index_path替换为me_home_index_url