I am trying to retrieve data/tables from this site: http://haydarov.net/mest.htm
and convert it into excel file and make it downloadable from my site.
tried nokogiri, writeexcel and some other libraries. I was able to download it as .cvs file with code below.
doc = Nokogiri::HTML(open("http://haydarov.net/mest.htm"))
<% csv = CSV.open("output#{Date.today}.csv", 'w') %>
<% #doc.xpath('//td[#class = "base"]').each do |row| %>
<% tarray = [] #temporary array %>
<% row.xpath('//td[#class = "base"]').each do |cell| %>
<% tarray << cell.text #Build array of that row of data. %>
<% end %>
<% csv << tarray #Write that row out to csv file %>
<% end %>
<% csv.close %>
what i need is to do kind of thing to export this data as excel file.
I have actually have poor code that might work with editing. from the url below, I do not know how to put it into excel file. I am using writeexcel gem/lib.
<% (open("http://haydarov.net/mest.htm") do |f| %>
<% workbook = WriteExcel.new('simple.xls') %>
<% worksheet = workbook.add_worksheet %>
# Row and column are zero indexed
<% row = 0 %>
<% f.read.each do |line| %>
<% col = 0 %>
<% line.chomp.split("\t").each do |token| %>
<% worksheet.write(row, col, token) %>
<% col += 1 %>
<% end %>
<% row += 1 %>
<% end %>
<% workbook.close %>
<% end %>
thanks for the help..
Related
I need piece of code for couple of times, and to follow DRY method (don't repeat yourself) i want to escape using this piece and retyping it for 5 times or more. So I want to define method what will contain both ruby and html, and ruby in html and js to, code. But I have problems with realization.
My html.erb look like this:
<% div_count = 1 %>
<div>
<% for post in #posts %>
<div class="post-on-main-page-<%= div_count %>"
id="js-count-<%= div_count_for_js %>">
<script>
$("#js-count-<%= div_count_for_js %>").click(function(){
window.location.href = "<%= post_url(post) %>";
});
</script>
<%= image_tag(post.picture.url) %>
<span><h5><%= post.theme %></h5></span>
</div>
<% end %>
<% div_count += 1 %>
<% if div_count == 4 %>
<% div_count = 1 %>
<% end %>
</div>
And I want create method, I think in application_helper.rb will do with this piece of code. What will look something like that:
def method_name(parameter)
<% div_count = 1 %>
<div>
<% for post in parameter %>
<div class="post-on-main-page-<%= div_count %>"
id="js-count-<%= div_count_for_js %>">
<script>
$("#js-count-<%= div_count_for_js %>").click(function(){
window.location.href = "<%= post_url(post) %>";
});
</script>
<%= image_tag(post.picture.url) %>
<span><h5><%= post.theme %></h5></span>
</div>
<% end %>
<% div_count += 1 %>
<% if div_count == 4 %>
<% div_count = 1 %>
<% end %>
</div>
end
The result what I want for html.erb
<%= method_name(#posts)%>
How can I make this to work?
You can move this code to some html. And when you want to use it, you just call render method.
<%= render 'html_file' %>
In my controller i find a uniq notebook and user name.
but i want to be able to check in my html code that it shows only one type of user.
in controller
def index
#allnotebooks = Note.uniq.pluck(:string, :notebook)
#notes = Note.all
end
in my html
<% #allnotebooks.each do |notebook| %>
<% if notebook.string == c_user.name %>
<option><%= notebook %></option>
<% end %>
<% end %>
notebook.string does not work. what am i missing
Also you can do in different way other than using pluck, using select you can do it
like-
In controller code-
def index
#allnotebooks = Note.uniq.select([:string, :notebook])
#notes = Note.all
end
in your html
<% #allnotebooks.each do |notebook| %>
<% if notebook.string == c_user.name %>
<option><%= notebook %></option>
<% end %>
<% end %>
Thanks!!!
Pluck returns an array.
Try notebook.first or notebook[0]
Docs here: http://apidock.com/rails/ActiveRecord/Calculations/pluck
Second example at the bottom applies here.
In your example it should be:
<% #allnotebooks.each do |notebook| %>
<% if notebook[0] == c_user.name %>
<option><%= notebook[1] %></option>
<% end %>
<% end %>
btw you might want to improve this by only loading notebooks where the string equals your user's name.
I am not a good ruby guy, You can write it like
<% #allnotebooks.each_with_index do |notebook, index| %>
<%= notebook[index].string %> => <%= notebook[index].notebook %>
<% end %>
for "a" unique notebook, you do you need to loop it?
just to say:
#allnotebooks.each{|notebook| <%= notebook[0] %> <%= notebook[1] %> }
But there can be better ways.
inside one of my view pages I'm using old fashion way of presenting data but I have problem in converting a string like "User.country.name" to a query statement.
I'm using a loop like below :
#columns = ['id','email','name','country.name']
table = User.all
<% table.each do |row| %>
<% #columns.each do |field| %>
<%= row[field] %>
<% end %>
<% end %>
The table shows almost all data except for column "country.name". I can't use constantize for country.name it gives me error. Any solution ? Thanks
Your User doesn't have an attribute country.name, it has an association country and that association has an attribute name.
You could set up a delegate:
class User < ActiveRecord::Base
delegate :name, to: :country, prefix: true
# ...
end
This creates a method User#country_name, returning country.name.
Now you can adapt your loop, using public_send to call the methods: (I've changed the variable names to make it clearer)
#methods = [:id, :email, :name, :country_name]
records = User.all
<% records.each do |record| %>
<% #methods.each do |method| %>
<%= record.public_send(method) %>
<% end %>
<% end %>
#users = User.select(:id, :email).joins(:countries)
<% #users.each do |user| %>
<%= user.id %>
<%= user.email %>
<%= user.countries %>
<% end %>
http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-joins
Wish it helps
I'm passing the results of a multi-select box to a page so that the selections can be shown on screen. As it's multi-select, the result can either be a scalar or an array reference. Is there a way of finding this out? I can't find anything online, but I thought there might be a .array or .array_ref token that could be used for validation.
I'm using Template Toolkit, Perl and Dancer.
So here is what I've got for a scalar:
<% IF multitext %>
Text: <% multitext %>
<% END %>
What I want is something like...
<% IF multitext %>
<% IF multitext.array_ref %> <!-- whatever works! -->
<% FOREACH text IN multitext %>
Text: <% text %>
<% END %>
<% ELSE %>
Text: <% multitext %>
<% END %>
<% END %>
If <%- multitext.0 -%> returns a non-zero value, it's an arrayref.
If <%- multitext.keys.size -%> returns a non-zero value, it's a hashref.
The way I usually handle it is to force it to be an array if it's a scalar, eg:
<%- SET items = multitext.0 ? multitext : [ multitext ];
FOREACH item IN items;
...
END; -%>
Several years later...
You could use the .list vmethod to guarantee it's an array e.g.
<% FOREACH text IN multitext.list %>
Text: <% text %>
<% END %>
See http://template-toolkit.org/docs/manual/VMethods.html#section_list
<% temp1= "secondary" %>
<% #query = "select credentials_id from credentials where credentials_id LIKE '%" + temp1 + "%'" %>
<% Request.connection.execute(#query) %>
<% if #query.blank? %>
<p>These are no secondary users still created </p>
<% else %>
<% #query.each do |c| %>
<%= label_tag(:credentials_id,c.credentials_id)%>
<%end%>
<%end%>
I am trying to execute the code above, that I have written in my "admin.html.erb" file, but I´m getting the following error:
undefined method 'each' for #
The #query variable is just an String. If you want to iterate over every single character on that string, you should use each_char method.
I really believe that you're iterating over the wrong variable. I think you want to iterate over the result of your query, not over your query characters! For that you should do something like:
<% #query_result = Request.connection.execute(#query) %>
<% #query_result.each do |c| %>
<%= label_tag(:credentials_id,c.credentials_id)%>
<%end%>