I have a ruby application right now that is storing data that is searchable. However, I wanted to create a file of the search results and was getting stuck. I found the file.open("given_name.txt", "w")
command but I'm having trouble populating it with search results.
The results are determined in my class for Mp3Meta
and returns from that as query in the active record base and populates it on screen in index.html.erb
. I just am lost as to how to address this or make sure that the data is passed so I can properly create the file.
I wanted to parse it in this format but I'm not sure on the operators, I'm new to ruby and rails.
def save_result
mp3 = Mp3Meta
file.open("result.txt","w")
file.put("#EXTM3U")
for.each.mp3(file.put("#MP3INF"+ mp3.length + "," + mp3.artist_name + "-" + mp3.ti开发者_Python百科tle + mp3.url))
end
end
mp3 = Mp3Meta
This is literally assigning the class Mp3Meta
to the variable mp3
. You are probably trying to create an instance of the class, which would be Mp3Meta.new
. If Mp3Meta
is an ActiveRecord class (which you seem to mention obliquely in the question), then you really need to use a query on the class instead to get an enumerable of the results.
Other lines in the code exhibit similar confusions, particularly the for.each.mp3
line, which is broken in at least 4 ways. :-)
I strongly recommend that you carefully go through a good Ruby tutorial, doing all the examples for yourself and trying minor modifications on your own. I recommend Programming Ruby. Once you understand Ruby pretty well, then start learning Rails.
If your question is how to create a random file on disk so that your file.open does not collide on multiple requests, then use tempfile to create the file.
http://ruby-doc.org/stdlib/libdoc/tempfile/rdoc/index.html
精彩评论