我已經添加了下面的代碼下載CSV:添加鏈接下載CSV使用Rails
<%= form_tag(account_export_courses_list_path(@account), :method => :get) do |f| %>
<a data-submit-form href="#" class="btn button-sidebar-wide"><i class="icon-export-content"></i> <%= t(:export_course_list_link, "Export Course List") %></a>
<% end %>
如果我做檢查元素,然後我能看到以下內容:
<form method="get" action="/accounts/1/courses/export_list" accept-charset="UTF-8"><div style="margin:0;padding:0;display:inline"><input type="hidden" value="✓" name="utf8"></div>
<a class="btn button-sidebar-wide" href="#" data-submit-form=""><i class="icon-export-content"></i> Export Course List</a>
</form>
而下面的代碼是在我的控制器補充說:
def export_courses_list
puts "===-----------------------------------==="
if authorized_action(@context, @current_user, :export_course_list)
puts "-------"
report = CsvService.new(@context.courses.active.order(:name, :created_at).all, {
fields:[
{attribute_name: "name", display_name: "Course Name"},
{attribute_name: "course_code", display_name: "Course Code"},
{attribute_name: "created_at", display_name: "Created Date"}
]
})
send_data report.to_csv, :filename => "Courses.csv"
end
end
我的路線是因爲:
match 'courses/export_list' => 'accounts#export_courses_list', :as => :export_courses_list
當我點擊鏈接「導出課程列表」後,我應該可以下載CSV。但我無法看到任何行動得到執行。提前致謝。
你的日誌說什麼? – usmanali
在日誌中,我點擊鏈接 – Neha
後沒有任何東西要麼改變你的'''標籤,以包括你指定爲'form'行動的鏈接,或者用'submit'類型的'input'替換這個標籤。 – usmanali