Ruby on rails Rails将哈希数组导出为csv
在myRuby on rails Rails将哈希数组导出为csv,ruby-on-rails,ruby,csv,Ruby On Rails,Ruby,Csv,在myshow路由中,有一个my_search路由(基本上是show#my_search),它以HTML格式显示数据的散列数组 我需要做的就是将@数据转储到我的渲染(或部分)中,并在视图中处理它们,使其成为一个带有嵌入式ruby的HTML表 但是,是否有一种简单的方法将相同的@数据发送到CSV文件?我是否必须再次获取@数据,并为其专门制定另一条路线?当显示页面localhost://show/my_search 编辑: @data看起来像: def my_search @data = [
show
路由中,有一个my_search
路由(基本上是show#my_search
),它以HTML格式显示数据的散列数组
我需要做的就是将@数据
转储到我的渲染
(或部分)中,并在视图中处理它们,使其成为一个带有嵌入式ruby的HTML表
但是,是否有一种简单的方法将相同的@数据发送到CSV文件?我是否必须再次获取@数据
,并为其专门制定另一条路线?当显示页面localhost://show/my_search
编辑:
@data
看起来像:
def my_search
@data = [ {"name"=>"John", "age"=>"21"}, {"name"=>"Amy", "age"=>"20"} ] # and other rows
# and do other stuff here
render "table_template"
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<% @data.each do |row| %>
<tr>
<td><%= row['name'] %></td>
<td><%= row['age'] %></td>
</tr>
<% end %>
</tbody>
</table>
@data=[{“name”=>“John”,“age”=>“21”},{“name”=>“Amy”,“age”=>“20”}]
app/controllers/show\u controller.rb
看起来像:
def my_search
@data = [ {"name"=>"John", "age"=>"21"}, {"name"=>"Amy", "age"=>"20"} ] # and other rows
# and do other stuff here
render "table_template"
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<% @data.each do |row| %>
<tr>
<td><%= row['name'] %></td>
<td><%= row['age'] %></td>
</tr>
<% end %>
</tbody>
</table>
在app/views/show/table_template.html中
def my_search
@data = [ {"name"=>"John", "age"=>"21"}, {"name"=>"Amy", "age"=>"20"} ] # and other rows
# and do other stuff here
render "table_template"
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<% @data.each do |row| %>
<tr>
<td><%= row['name'] %></td>
<td><%= row['age'] %></td>
</tr>
<% end %>
</tbody>
</table>
视图:将url添加到localhost://my_search_export.csv
坏的是它再次加载数据,好的是工作流程很简单。我仍在寻找更好的解决方案
注意:此网站可能有不同的用户同时运行,因此保留全局变量对我来说不太合适。如果需要,您可以打开CSV并在渲染之前写入它:
@data = { first_stuff: ['a', 'b', 'c'], second_stuff: [1, 2, 3] }
CSV.open('some/file.csv', 'w') do |csv|
@data.each_pair do |key, value|
csv << value
end
end
render json: @data
@data={first_stuff:['a','b','c',second_stuff:[1,2,3]}
CSV.open('some/file.CSV','w')do | CSV|
@data.u每对do |键、值|
csv更新2016年6月20日:我当前的工作范围:
app/controllers/show_controller.rb:
定义我的搜索
获取数据
#在这里做其他事情
render "table_template"
结束
定义我的搜索导出
获取数据
格式。每个do |格式|
#访问时呈现CSV格式的内容localhost://my_search_export.csv
.....
结束
结束
私人的
def get#U data#写入@data
@数据=[{“姓名”=>“约翰”,“年龄”=>“21”},{“姓名”=>“艾米”,“年龄”=>“20”}]
结束
在视图中:将url添加到localhost://my_search_export.csv.
坏的是它再次加载数据,好的是工作流程很简单。我仍在寻找更好的解决方案
另外,这个网站可能有不同的用户同时运行,所以保留一个全局变量对我来说并不合适。可能重复感谢Seth,但是当显示localhost://show/my_search
可能会有帮助,但这只允许转到localhosts://some/path/file.csv
这意味着它在l中不显示网页ocalhosts://some/path/file.csv
谢谢您的帮助!我对这个问题作了更多的描述。如果此函数具有呈现json:@data
,那么它将不会呈现任何html页面吗?显示页面localhost://show/my_search
?哦,你的意思是在呈现网页之前将CSV文件(可能在视图中添加指向some/file.CSV
路径的链接)写入本地路径(可能在public
文件夹?)吗?哦,如果你呈现的是HTML页面,您的控制器方法的实例变量应该只在视图中可用。换句话说,只要正常呈现页面,@data
应该可以在模板中使用。至于您的第二条评论,您可以将CSV的文件路径设置为实例变量(即@CSV\u path='some/file.CSV'
),然后它也可以在您的视图中用于链接目的。谢谢!因此,我只需将CSV写入'some/file.CSV'
的本地副本,然后将其链接到用户?这将是一个很好的解决办法,但我想知道它是否可以提供一次性下载链接,而不会在网站上存储太多东西@杰姆施莱斯