Javascript 404 on rails js调用,可能路由定义不正确

Javascript 404 on rails js调用,可能路由定义不正确,javascript,ruby-on-rails,ajax,Javascript,Ruby On Rails,Ajax,我在单击一个链接以将某些数据加载到分部文件时遇到以下javascript错误: GET http://localhost:3000/activate_foo_path 404 (Not Found) 我有以下链接: <%=link_to foo.name, "activate_#{foo.name.gsub(' ', '_').downcase}_path", :remote => true %> 控制器方法: def activate_foo @bars =

我在单击一个链接以将某些数据加载到分部文件时遇到以下javascript错误:

GET http://localhost:3000/activate_foo_path 404 (Not Found) 
我有以下链接:

<%=link_to foo.name, "activate_#{foo.name.gsub(' ', '_').downcase}_path", :remote => true %>
控制器方法:

 def activate_foo
    @bars = Bar.includes(:fobs).where(:fobs => {:id => 1})
    respond_to do |format|
      format.js
    end
  end
我有一个文件views/bar/activate_foo.js,内容如下:

$("#bar-listings").html("<%= escape_javascript(render partial: 'foo', locals: { bars: @bars } ) %>");
$(“#条形列表”).html(“”);
以下是应更换的div:

<div id="data-service-listings">
    <p>Whatever</p>
</div>

随便

然后我有ufoo.html.erb:

<% @bars.each do |bar| %>
   Do stuff with <%= bar %>
<% end %>

利用
我觉得我的路线写错了。我试着把它弄得乱七八糟,到处移动文件

另外,请随时就我使用的策略提供总体建议

谢谢


编辑:更新链接定义。

如果要链接到特定资源,通常需要在调用
foo\u path
时包含模型实例。下面的代码是实现这一点的更具讽刺意味的方式:

routes.rb

resource :businesses

get 'activate_billing/:id' => 'billing#activate', :as => :activate_billing
class BusinessesController < ApplicationController
    def index
        @businesses = Business.all
    end
end
<% @businesses.each |business| do
    <%= link_to business.name, activate_billing_path(business), :remote => true %><br>
<% end %>
class BillingController < ApplicationController
    def activate
        business = Business.find(params[:id])
        business.billing.activate
        render :success
    end
end
我的Rails路线有些生疏,因此
activate\u billing
的路线可能不正确

企业_controller.rb

resource :businesses

get 'activate_billing/:id' => 'billing#activate', :as => :activate_billing
class BusinessesController < ApplicationController
    def index
        @businesses = Business.all
    end
end
<% @businesses.each |business| do
    <%= link_to business.name, activate_billing_path(business), :remote => true %><br>
<% end %>
class BillingController < ApplicationController
    def activate
        business = Business.find(params[:id])
        business.billing.activate
        render :success
    end
end

除非您需要呈现特定的内容,否则控制器只返回一个200OK响应。问题中包含的代码很难判断。

您使用两个字符串作为参数调用link\u to:

<%=link_to foo.name, "activate_#{foo.name.gsub(' ', '_').downcase}_path", :remote => true %>
true%>
在本例中,第一个参数是:foo.name,这很好

第二个参数是“activate#{foo.name.gsub(''''.''.'.downcase}}_path”,它将只是一个字符串,其方式与“”是字符串的方式相同

您想要的是“activate_#{foo.name.gsub(''''.'.''.''.downcase}}_path”的结果,所以实现这一点的一个丑陋的方法是评估它:

<%=link_to foo.name, eval("activate_#{foo.name.gsub(' ', '_').downcase}_path"), :remote => true %>
true%>

<>但是这允许各种XSS和危险,所以我建议你考虑重新考虑这个功能。

我犯了把链接的HTML输出放在我的问题中的错误。请查看更新。我实际上已将链接更改为:
business\u domain.id),:remote=>true%>
。我仍然会犯类似的错误。我已将routes.rb中的路径更新为
get“activate_billing”=>“data_services#activate_billing”,:as=>:activate_billing