我正在尝试将变量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
答案 0 :(得分:1)
find_by_sql(["SELECT s.supplier, s.productcode, s.description, CAST(SUM(l.amount) as UNSIGNED) AS amount
FROM软件 LEFT JOIN许可证l ON s.id = l.software_id 什么s.supplier =?和l.amount&gt; 0 GROUP BY s.supplier,s.vendor,s.title,s.edition“,params [:supplier])
我假设find_by_sql在你的控制器中,“export_supplier”方法。 在这里你应该得到params [:supplier]