Forum: Ruby on Rails Export CSV

6983e7aaea82078286d067a9d4e688d4?d=identicon&s=25 Daynthan Kabilan (Guest)
on 2013-11-08 07:26
(Received via mailing list)
Hi friends,

i am using csv export on my application. in my view file code is


<%- headers = ["Transaction ID", "Activity ID", "Product Name","Customer
Name", "Ticket code"] -%>
<%= CSV.generate_line(headers).strip %>
<%if !@trans.nil? && @trans.length > 0 %>
  <% @trans.each do |tran|%>

<% if !tran.transaction_id.nil? &&
tran.transaction_id.present?%><%tran_id
= tran.transaction_id%><%else%><%tran_id = "-"%><%end%>
<% if !tran.activity_id.nil? && tran.activity_id.present?%><%tran_actid
=
tran.activity_id%><%else%><%tran_actid = "-"%><%end%>
<% if !tran.activity_name.nil? &&
tran.activity_name.present?%><%tran_actname =
tran.activity_name%><%else%><%tran_actname = "-"%><%end%>
<% if !tran.customer_name.nil? &&
tran.customer_name.present?%><%cus_name =
tran.customer_name%><%else%><%cus_name = "-"%><%end%>
<% if !tran.ticket_code.nil? && tran.ticket_code.present?%><%tran_code =
tran.ticket_code%><%else%><%tran_code = "-"%><%end%>

    <%- row = [ tran_id, tran_actid,  tran_actname, cus_name, tran_code
]
-%>
     <%#= CSV.generate_line(row).html_safe.strip%>
  <%end%>
<%else%>
  Sorry we found no results
<%end%>

now i got 5 extra empty rows how can avoid the empty rows.

thanks for advance.
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-11-08 11:12
(Received via mailing list)
On 8 November 2013 06:24, Daynthan Kabilan <dayanthan86@gmail.com>
wrote:
>
> tran.ticket_code%><%else%><%tran_code = "-"%><%end%>
>
>     <%- row = [ tran_id, tran_actid,  tran_actname, cus_name, tran_code ]
> -%>
>      <%#= CSV.generate_line(row).html_safe.strip%>
>   <%end%>
> <%else%>
>   Sorry we found no results
> <%end%>
>
> now i got 5 extra empty rows how can avoid the empty rows.

Do you mean that @trans.count is 5 less than the number of rows
generated?  If you, for example, include something like
puts "@trans.count = #{@trans.count}"
in the code then this will be displayed in the server window.

Colin
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-11-09 23:49
(Received via mailing list)
On Nov 8, 2013, at 12:24 AM, Daynthan Kabilan <dayanthan86@gmail.com>
wrote:

> <% if !tran.transaction_id.nil? && tran.transaction_id.present?%><%tran_id =
tran.transaction_id%><%else%><%tran_id = "-"%><%end%>
> <%end%>
>
> now i got 5 extra empty rows how can avoid the empty rows.
>
> thanks for advance.

Seems like this would be a lot easier to deal with as a helper than all
this code in the view in ERB.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.