Saturday, December 11, 2010
Fixing libmysql.dll issue in Rails
1. type the command in the console
gem install mysql
(this is for windows, if you are using linux os add sudo at the beginning)
2. Copy the libmysql.dll found in the mysql installation directory (mysql\bin) and paste it in your ruby installation directory (ruby\bin).
3. Restart the server.
thats all, it will work now.
Wednesday, December 1, 2010
HTML mailto attribute
The HTML mailto is a quick way to add the facility of receiving feedback from visitor on the web site. when the visitor clicked the HTML mailto, It lanuches their email program with new email window.
Note: HTML mailto assumes that the visitor has configured an email client (Outlook Express, Netscape Messenger, Thunderbird or any other) to send emails.
The Basic form HTML mailto requires an email address.
Its looks like:
<a href="mailto:info@openshell.in">Feedback Form</a>
The Complex form HTML mailto by adding an email subject and the email body so that it looks a little more professional.
Its look Like:
<a href="mailto:info@openshell.in?subject=Feedback for Openshell.in&body=Thanks for Provinding useful Tips">Feedback Form</a>
In addition to the body and subject, we can also provide HTML mailto with CC (Carbon Copy) and BCC (Blind Carbon Copy). This, as you would have guessed, requires us to append these values to the HTML mailto attribute just like we had done for body and subject.
Its look Like:
<a href="mailto:info@openshell.in?subject=Feedback for Openshell.in&body=Thanks for Provinding useful Tips&cc=anotheremailaddress@anotherdomain.com&bcc=onemore@anotherdomain.com">Feedback Form</a>
HTML mailto is a quick and easy way for beginners and who don't know server-side programming languages (such as JSP, PHP, ROR etc.) to add a link on their web site for receiving visitor feedback.
Tips to inspect the code in Rails
- Inspect
It inspects the value. It formats the array with square brace, hash with curly braces with arrows between the key value pairs
inspecting parameter.
- Type
It identifying the type of the instance variable.
- Debug
It give nice, easy to read inspection of the value and displayed in a formatted way.
These are all some good ways to see what is happening inside your application code.
Example
action inside the controller
def current_status
@my_array = [1,'king', 200]
@my_hash = {'name'=> 'Pinto'}
end
in view
<%= @my_array.inspect %>
<%= @my_hash.inspect %>
<%= params.inspect %>
Result:
[1,"king", 200]
{'name'=> 'Pinto'}
{"action"=>current_status, "controller"=>sample}
To inspect the data type use 'type':
in view
<%= @my_array.type %>
<%= @my_hash.type %>
Result:
Array
Hash
Another inspection method 'debug' :
in view
<%= debug(@my_array) %>
<%= debug(@my_hash) %>
Result:
---
-1
-king
-200
---
name: Pinto
Sunday, November 28, 2010
Ruby Symbols Description
Matter related to the old and new Ruby programmer, and Ruby's symbols are confusing sometimes or always. Smoothly before this, can not get used is pretty. People learn about the Ruby language is often Rubionreiruzu learn from the project. The Rubionreiruzu symbol is everywhere. There really is everywhere. So, pay attention to the concept of a symbol of ruby, it's important to remember that.
Ruby is an instance of a symbol of class symbol. Put before the identifier symbol following the colon, ": name", ": id" or ": user". Ruby Symbol class includes a method of a class.
- all_symbols - Returns an array of all the symbols currently in Ruby’s symbol table.
- id2name - Returns the name or string corresponding to sym:, name.id2name the "name" back.
- inspect - Literally back to the symbol.
- to_i - Return a unique integer to each symbol. This method never raises an exception.
- to_s - id2name as same. Returns the string.
- to_sym - Returns the symbol corresponding to string.
Debugging Rails
Using the Ruby logger class from inside your controllers, you can also log your own messages directly into the log file from your code like:
class ReportController < ActionController::Base
def destroy
@report = Report.find(params[:id])
@report.destroy
#Debug Log file
logger.info("#{Time.now} Destroyed Report ID ##{@report.id}!")
end
end
These are the available log levels, :debug, :info, :warn, :error, and :fatal, corresponding to the log level numbers from 0 up to 4 respectively.
Description:
logger.debug
The DEBUG level designates fine-grained informational events that are most useful to debug an application.
logger.info
The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.
logger.warn
The WARN level designates potentially harmful situations.
logger.error
The ERROR level designates error events that might still allow the application to continue running.
logger.fatal
The FATAL level designates very severe error events that would presumably lead the application to abort.
To write in the current log use the logger.(debug|info|warn|error|fatal) method from within a controller, model or mailer.
The default Rails log level is info in production mode and debug in development and test mode.
Finders are great but be careful
Finders are very pleasant to use, enable you to write readable code and they don’t require in-depth SQL knowledge. But the nice high level abstraction come with a computational cost.
Follow these rules of thumb:
- Retrieve only the information that you need. A lot of execution time can be wasted by running selects for data that is not really needed. When using the various finders make sure to provide the right options to select only the fields required (:select), and if you only need a numbered subset of records from the resultset, opportunely specify a limit (with the :limit and :offset options).
- Don't kill your database with too many queries, use eager loading of associations through the include option:
Avoid dynamic finders like MyModel.find_by_*. While using something like User.find_by_username is very readable and easy, it also can cost you a lot. In fact, ActiveRecord dynamically generates these methods within method_missing and this can be quite slow. In fact, once the method is defined and invoked, the mapping with the model attribute (username in our example) is ultimately achieved through a select query which is built before being sent to the database. Using MyModel.find_by_sql directly, or even MyModel.find, is much more efficient.
- Be sure to use MyModel.find_by_sql whenever you need to run an optimized SQL query. Needless to say, even if the final SQL statement ends up being the same, find_by_sql is more efficient than the equivalent find (no need to build the actual SQL string from the various option passed to the method). If you are building a plugin that needs to be cross-platform though, verify that the SQL queries will run on all Rails supported databases, or just use find instead. In general, using find is more readable and leads to better maintainable code, so before starting to fill your application with find_by_sql, do some profiling and individuate slow queries which may need to be customized and optimized manually.
Optimize your Ruby code
This may seem obvious, but a Rails application is essentially ruby code that will have to be run. Make sure your code is efficient from a Ruby standpoint. Take a look at your code and ask yourself if some refactoring is in order, keeping in mind performance considerations and algorithmic efficiency. Profiling tools are, of course, very helpful in identifying slow code, but the following are some general considerations (some of them may appear admittedly obvious to you):
- When available use the built-in classes and methods, rather than rolling your own;
- Use Regular Expressions rather than costly loops, when you need to parse and process all but the smallest text;
- Use Libxml rather than the slower REXML if you are processing XML documents;
- Sometimes you may want to trade off just a bit of elegance and abstraction for speed (e.g. define_method and yield can be costly);
- The best way to resolve slow loops, is to remove them if possible. Not always, but in a few cases you can avoid loops by restructuring your code;
- Simplify and reduce nested if/unless as much as you can and remember that the operator ||= is your friend;
- Hashes are expensive data structures. Consider storing the value for a given key in a local variable if you need to recall the value a few times. More in general, it’s a good idea to store in a variable (local, instance or class variable) any frequently accessed data structure.
Group operations in a transaction
ActiveRecord wraps the creation or update of a record in a single transaction. Multiple inserts will then generate many transactions (one for each insert). Grouping multiple inserts in one single transaction will speed things up.
Insead of:
my_collection.each do |q|
Report.create({:phrase => q})
end
Use:
Report.transaction do
my_collection.each do |q|
Report.create({:phrase => q})
end
end
or for rolling back the whole transaction if any insert fails, use:
Report.transaction do
my_collection.each do |q|
report = Report.new({:phrase => q})
report.save!
end
end
Monday, November 22, 2010
Refresh / Redirection
<HEAD>
<meta http-equiv='refresh' content='2;url='file_name or URL'>
</HEAD>
// content = time (second)
// file_name = name of file you want to refresh or redirect
Many websites use this scripts to redirect to another page or refresh the same page.
For example: My website updates in every 10 minutes and I want to show user my latest content then I put this script to my page and set it refreshs in every 10 minutes