ruby on rails - How to include parameters when exporting db to csv? -



ruby on rails - How to include parameters when exporting db to csv? -

i'm trying export set of records csv file in redmine plugin. records stored in mysql database, has column project_id tie each record particular project. want export records match value project_id.

app/controllers/foo_controller.rb:

before_filter :find_project def index @foos = foo.order(:id).where("project_id = ?", @project.id) # csv request throws nomethoderror here because @project nil. respond_to |format| format.html format.csv { send_data @foos.to_csv(:col_sep => ",") } end end private def find_project if params[:project_id].present? @project = project.find params[:project_id] end end

init.rb

redmine::plugin.register :foo_plugin # info stuff... # module/permission stuff... menu :project_menu, :foos, {:controller => 'foo_con', :action => 'index'}, :caption => 'foos', :param => :project_id end

from can tell, issue how csv request handled in rails. when index page first loaded, project_id auto included in params array. when reloads index handle csv request, project_id doesn't added params array. there way alter behavior?

i don't see links creating .csv request. include :format => 'csv' in menu link.

also, may need info in send_data export. example:

format.csv { send_data(@foos.to_csv, :type => 'text/csv; header=present', :filename => 'issues.csv') }

on side note, redmine has built in csv exporting valid @query. can take advantage doing this:

cond = "project_id null" cond << " or project_id = #{@project.id}" if @project @query = issuequery.where(cond) respond_to |format| # ... format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'issues.csv') } # ...

the query_to_csv function within queries_helper.rb:

def query_to_csv(items, query, options={}) encoding = l(:general_csv_encoding) columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns) query.available_block_columns.each |column| if options[column.name].present? columns << column end end export = fcsv.generate(:col_sep => l(:general_csv_separator)) |csv| # csv header fields csv << columns.collect {|c| redmine::codesetutil.from_utf8(c.caption.to_s, encoding) } # csv lines items.each |item| csv << columns.collect {|c| redmine::codesetutil.from_utf8(csv_content(c, item), encoding) } end end export end

ruby-on-rails csv redmine

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -