我试图将变量传递supplier
到我的 sql 语句的 where 子句中。它将变量保存为当我单击链接时,它会在url
. 我只是不知道在语句中放入什么以使其从视图中获取变量。
模型:
def self.find_supplier
find_by_sql(["SELECT s.supplier, s.productcode, s.description, CAST(SUM(l.amount) as UNSIGNED) AS amount
FROM softwares s
LEFT JOIN licenses l ON s.id=l.software_id
WHERE s.supplier = ? AND l.amount > 0
GROUP BY s.supplier, s.vendor, s.title, s.edition", :supplier)
end
看法:
<% @softwares.each do |l| %>
<li>
<%= link_to "#{l.supplier}", :controller =>'softwares', :action => 'export_supplier', :supplier => l.supplier %>
</li>
<% end %>
控制器:
def export_supplier
@software = Software.find_supplier
software = CSV.generate do |csv|
# for the headers of the csv file
csv << ["Quantity", "Item Number", "Item Description"]
# the chosen rows from the database
@software.each do |s|
csv << [s.amount, s.productcode, s.description, s.supplier]
end
end
send_data(software, :type => 'text/csv', :filename => 'software.csv')
end