我在将值下载到CSV文件时遇到了一些问题。我有两个模型,商业区和房产。市中心有很多房产。每家酒店在市中心都有一家。我的问题是,我所做的事情,我已经能够下载所有属性,这是很酷!但我正在苦苦挣扎的是只下载市中心所有的房产。
到我最初要下载的所有属性
在我的属性控制器中:
  def index
    @properties = Property.all
    respond_to do |format|
      format.html
      format.csv { send_data @properties.to_csv }
    end
  end在我的属性模型中
  def self.to_csv
    attributes = %w(id name owner_first_name created_at)
    CSV.generate(headers: true) do |csv|
      csv << attributes
      all.each do |property|
        csv << property.attributes.values_at(*attributes)
      end
    end
  end在我看来,我有。
= link_to "download CSV", downtown_properties_path(:format => :csv), class: "button"  同样,这是可行的,但由于我的关联,我可以添加链接的视图只能在downtowns/1/properties中,这基于我将代码放在index中,这实际上很有意义
我尝试了一些类似的东西
@download_properties = Property.where(downtown: @downtown_id)
respond_to do |format|
  format.html
  format.csv { send_data @download_properties.to_csv }
end但是,在这一点上还没有定义@downtown_id。所以我认为在downtown模型中解决方案更好?
如果任何人对此有任何想法并能帮助我,我将不胜感激!
发布于 2020-03-03 09:03:25
它不应该是:
@download_properties = Property.where(downtown: params[:id])或
@download_properties = Downtown.find(params[:id]).propertieshttps://stackoverflow.com/questions/60498619
复制相似问题