Rails: Display an clickable image, and pass parameters
Essentially I'm dealing with 3 things: (*the first 2 work)
- An Action called Index(), which accepts a user_id parameter.
- A CSV export feature
- A needed link to the CSV export file, from the Index view; which ideally utilizes the link_to method.
The first two work perfectly and I'm stumbling on generating a link using the link_to method because the CSV file needs to be scoped to the proper user using a user_id parameter.
Here is my Controller Code:
def index
@pro = Pro.find_by_user_id(params[:user_id])
csv_code = CSV.generate do |csv|
@pro.accounts.each do |account|
csv << [account['name'],account['number']]
end
end
respond_to do |format|
format.html
format.csv { render :csv => csv_code}
end
end
index.html.haml: * notice I'm missing 开发者_运维百科a parameter 1234, that this needs to generate a csv
= link_to image_tag "export.png", {:action => :index.csv}
The Url that works to generate a CSV:
http://localhost:3000/.csv?user_id=1234
I'm sure this is a super easy problem to solve, and I've spent an hour overlooking the obvious solution. Thank you in advance!
Just add the "(" to the image tag.
= link_to image_tag ("export.png"), {:action => :index.csv}
精彩评论