SyntaxHighlighter is a source code syntax highlighting plugin to help a developer/coder to post code snippets online with ease and and without having to worry about applying format. It is a free JavaScript tool for source code Syntex highlighting , It doesn’t care what you have on your server.
The idea behind SyntaxHighlighter is to allow insertion of colored code snippets on a web page without relying on any server side scripts.
Place your code on the page and surround it with <pre> tag. Set name attribute to code and class attribute to one of the language aliases you wish to use.
<div class=”dp-highlighter“> </div>
<pre name=”code” class=”ruby” style=”display: none;“>
… some code here …
</pre>
An alternative to <pre> is to use <textarea> tag.
<textarea name=”code” class=”ruby”>
… some code here …
</textarea>
The CSV text-file format is a common choice for both import and export when performing data migrations. What if you want to import CSV files within a Rails application?
Here is the code that allow you to upload CSV data onto the MySQL database from the web interface
This code save data direct to database without saving it to temp file
Controller:
def csv_import
@parsed_file=CSV::Reader.parse(params[:dump][:file])
n=0
@parsed_file.each do |row|
c=CustomerInformation.new
c.job_title=row[1]
c.first_name=row[2]
c.last_name=row[3]
if c.save
n=n+1
GC.start if n%50==0
end
flash.now[:message]=CSV Import Successful, #{n} new records added to data base”
end
So if you are trying to do a multiple select of checkboxes and using habtm in your project, but when you submit the form, only one value was available in your controller. While you try to edit records in database but because of some error you get back to the pre field form and you found that the checkboxes checked by you gone ,then heres the solution
Model:
has_and_belongs_to_many :intrests
end
Controller Code:
def edit
@customer=Customer.find_by_id(params[:id]) if params[:id]
if @customer
if request.post?
if @customer.update_attributes(customer)
flash.now[:message]=Update successfully
end
end
else
flash[:message]=Page requested by you does not exists
end
end
end
Your View:
Helper method
View generate a checkbox for every interest(all_interest=Interest.find(:all)). The name of the input is significant obviously. The trailing [] on the name means the end result will be the list of checked ids. This list will be stored on the @params['customer'] hash with the key interest_ids. When the controller calls @customer.update_attributes(@params[:customer]), it tries to call @customer.key= for each of the keys on @params[:customer]. Whats important to realize is that these keys dont have to actually be attributes on the Customer model. All thats important is that theres a key= method on the model. Model automatically contains a collection_ids= method for habtm and has-many associations.
This method will load the objects identified by the ids and call the interest=(list) method on the model with the freshly loaded list. This method in turn, will compare the list to the current list of interests and delete/add interests as necessary. read more »
@customers=CustomerInformation.find(:all)
report = StringIO.new
CSV::Writer.generate(report, ,) do |title|
title << [Id, Title, Job Title, First Name, Last Name]
@customers.each do |c|
title << [c.id, c.title, c.job_title, c.first_name, c.last_name]
end
end
report.rewind
send_data(report.read,:type => text/csv; charset=iso-8859-1; header=present,:filename => report.csv, :disposition =>attachment, :encoding => utf8)
end