[英]how to export csv file checkbox value into table in ruby on rails?
school_type col 復選框數據將 csv 文件導出到表中。 我在我的代碼下試過
學生.rb
# for csv file
def self.to_csv
attributes = %w{id status email temp_registration_date full_name full_name_kana main_registration_date email2 postal_code1 postal_code2 address1 address2 tel_main tel_sub sex school_type school_name school_name_initial department_name faculty_type graduation_month
industry_id industry_most_id occupation_id occupation_most_id desired_work_region_code club_flg school_club_name school_club_type club_name lab_name major_field
research_subject_title research_subject_summary interested_companies personal_summary how_known how_known_other mail_magazine_flg}
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |student|
csv << [student.id,student.name, student.sex, if (student.school_type == 1)
大學
elsif (student.school_type == 2)
大學院(修士)
elsif (student.school_type == 3)
大學院(博士)
elsif (student.school_type == 4)
短大
elsif (student.school_type == 5)
専門學校
elsif (student.school_type == 6)
高専
end]
end
end
我有我的桌子
你可能需要更多這樣的東西:
attributes = %w{id status email...}
CSV.generate(headers: true) do |csv|
# Build headers
csv << attributes
all.each do |student|
# use a variable to set the school_type string
school_type_text = if (student.school_type == 1)
"大學"
elsif (student.school_type == 2)
"大學院(修士)"
elsif (student.school_type == 3)
"大學院(博士)"
elsif (student.school_type == 4)
"短大"
elsif (student.school_type == 5)
"専門學校"
elsif (student.school_type == 6)
"高専"
end
csv << [student.id, student.name, student.sex, school_type_text]
end
end
我在OpenStruct.new({school_type: 2, id: 1, name: 'test'})
(不在 Rails 中)上運行它,output 是:
"id,status,email,temp_registration_date,full_name,full_name_kana,main_registration_date,email2,postal_code1,postal_code2,address1,address2,tel_main,tel_sub,sex,school_type,school_name,school_name_initial,department_name,faculty_type,graduation_month,industry_id,industry_most_id,occupation_id,occupation_most_id,desired_work_region_code,club_flg,school_club_name,school_club_type,club_name,lab_name,major_field,research_subject_title,research_subject_summary,interested_companies,personal_summary,how_known,how_known_other,mail_magazine_flg
\n1,test,something,大學院(修士)\n"
而不是每次都比較價值你可以有一個 hash
SCHOOL_TYPE = {1=>'大學', 2=>'大學院(修士)', 3=>'大學院(博士)', 4=>'短大', 5=>'専門學校', 6=>'高専'}
做這樣的事情
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |student|
csv << [student.id, student.name, student.sex, SCHOOL_TYPE[student.school_type]]
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.