Hi, I Want to simply print out 2d array elements. Method is located in
Helper module. Calling helper method from jQuery. At the moment I’m not
getting output (=> 0…3) I tried to do iterate through array via .each()
method, but it would just print en entire array, however i want to
access each element and convert it either to string or integer. can
someone point me to possible solution? thanks!
#app/helpers/controller_helper.rb
def get_array
database = SQLite3::Database.open("test.db")
result = database.execute("select * from table1")
print_array(result)
end
def print_array(array)
for i in 0..array.length
puts array[i][i]
end
end
-------------------------------------------------------------------
#app/views/controller/file.js.erb
if($('.check_box1').is(':checked')){
$("#div2").html("<%= get_array%>")
}
You dont want to use puts in there. You want to return the value so it
gets evaluated in the erb code. puts returns nil, which means you get
nothing inside the html element.
Im not exactly sure from that method what you want inside div2 either.
Heres what Id suggest:
def get_array()
sqlite stuff as you have it
format_table(result)
end
def format_table(data)
out = “
\n”
out += data.map do |row|
format_row(row)
end.join("\n")
out += “\n
\n”
end
def format_row(row)
out = “
”
out += row.map do |cell|
format_cell(cell)
end.join
out += “
”
end
def format_cell(cell)
“
#{cell}
”
end
This will output html code, which seems like what youd want at that
point.
You dont want to use puts in there. You want to return the value so it gets
evaluated in the erb code. puts returns nil, which means you get nothing inside
the html element.
def format_table(data)
format_cell(cell)
end.join
out += “”
end
def format_cell(cell)
“
#{cell}
”
end
This will output html code, which seems like what youd want at that point.
then i decided it was too ‘wet’, and refactored:
def wrap(s, tag, attr={}, &b)
s = [s] unless s.is_a? Array
attr = attr.empty? ? ‘’ : " #{attr.map{|k,v| “#{k}=’#{v}’”}.join("
“)}”
out = “<#{tag}#{attr}>”
out += s.inject("") {|m,o| m+="#{block_given? ? (yield o) : o}"}
out += “</#{tag}>”
end
def format_table(data)
wrap(data,“table”,border: 1) do |row|
wrap(row,“tr”) do |cell|
wrap(cell,“td”)
end
end
end
Hi, once again thanks for you code contribution, that helped me to see
problem from a different perspective a little bit. I liked both of your
code snippets, and 1st one in particular (since i actually understand
it)
There is only one issue though, if I try to run code below on trough
jQuery (when the checkbox is checked) it wouldnt really execute
anything.
That’s what I had problem before, and I know the problem is not with
jQuery method. I cant easily modify code to
$("#div2").html("<%= ‘hello’%>") and it would print ‘hello’ in that div.
So, there is something that jQuery doesnt see in iether get_array or
format_table method. I’ve tried to merge all the 4 methods into one and
run it as one method, but it still wouldnt work. Would you please check
it one again, and let me know what could be changed?
And yes, I just want to output the result of parsed array into html
code. I dont really care how, it’s just I know only Jquery .html()
method and it seems relatively simple.
Thank you very much
def get_array()
sqlite stuff as you have it
format_table(result)
end
def format_table(data)
out = “
\n”
out += data.map do |row|
format_row(row)
end.join("\n")
out += “\n
\n”
end
def format_row(row)
out = “
”
out += row.map do |cell|
format_cell(cell)
end.join
out += “