我的目标是用相同的控制器调用一个html.erb和一个js.erb文件,但是它只调用我的html文件。
我的js不叫。
控制器/类别控制器
def index
respond_to do |format|
format.html
format.js
end
@categories = Category.order('name ASC')
@category = params[:category]
end视图/类别/index.html.erb
<% @categories.each do |c| %>
<%= link_to c.name, show_category_path(category: c.id), :id => "btn-filter#{c.id}" %>
<% end %>视图/类别/index.js.erb(问题在这里,此文件不被调用)
alert("test");
$("#btn-filter<%=@category%>").attr("class","active");发布于 2017-04-22 14:39:16
控制器以请求所要求的格式进行响应,因此,您的链接要求的是HTML,而不是JS,因此控制器使用.html.erb进行响应。如果您添加了一个电话,如下所示:
<%= link_to c.name, show_category_path(category: c.id), :id => "btn-filter#{c.id}", remote: true %>您的请求将请求JS (因为有remote: true属性),控制器将使用.js.erb进行响应。
发布于 2017-04-22 14:40:35
You should add remote: true option
<% @categories.each do |c| %>
<%= link_to c.name, show_category_path(category: c.id),remote:true, :id => "btn-filter#{c.id}" %>
<% end %>它将自动找到index.js.erb文件。
发布于 2017-04-23 06:21:58
只需向remote: true属性添加link_to即可。
https://stackoverflow.com/questions/43559855
复制相似问题