如何将变量从ajax文件传递到视图?

时间:2016-04-14 14:30:55

标签: ruby-on-rails ajax ruby-on-rails-4

我的问题是:我可以根据需要呈现ajax请求,但我想将我的ajax文件(products.js.erb)中的@products变量传递给名为index.js的{​​j}文件,{{1我从哪里发送Ajax请求。

类别控制器索引操作对应的视图/ category / index.html.erb我向产品控制器列表产品操作发送Ajax请求的地方。

category.html.erb

这是我的products.js.erb内容。它工作正常,它正确地呈现html内容,但我也想在index.js传递或访问js变量或json我的@product变量(属于类别控制器索引操作视图)。

class CategoriesController < ApplicationController
   def index
      @categories = Category.all
   end
end 

class ProductsController < ApplicationController

    def list_products 
      group_id = params[:group_id]

      @products = Product.where(GROUP: group_id)

      respond_to do |format|
      format.js { render :file => "app/views/category/products.js.erb" } 
    end
   end
end

我也希望在我的js文件中访问 <% href = "#"%> <% class_html = 'list-group-item'%> $('#product_list').empty(); <% @products.each do |product| %> $('#product_list').append('<%=j("<a href=#{href} class=#{class_html}>#{product.ADI}</a>").html_safe %>'); <% end %> 变量(@products) order.html.erb

order.js

简而言之,我想使用我通过Ajax获取请求获得的内容,在本例中是@products变量在我的index.js文件中。

0 个答案:

没有答案
相关问题