2

我试图将变量传递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
4

1 回答 1

1
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", params[:supplier])

我假设 find_by_sql 在您的控制器中,“export_supplier”方法。在这里你应该得到 params[:supplier]

于 2012-10-11T12:13:04.240 回答