Ruby on Rails Programming Tips

Creating a Model on Rails


Image Source: intertwingly.net

This is not based on a database application, so we can pretty much keep the name, which is comfortable for us. Lets take for example if we have to make a DataFile model.

C:\ruby> ruby script/generate model DataFile

  • exists app/models/
  • exists test/unit/
  • exists test/fixtures/
  • create app/models/data_file.rb
  • create test/unit/data_file_test.rb
  • create test/fixtures/data_files.yml
  • create db/migrate
  • create db/migrate/001_create_data_files.rb

Now we will create a method called save in data_file.rb model file. This method will be called by the application controller.

  • class DataFile < ActiveRecord::Base
  • def self.save(upload)
  • name = upload['datafile'].original_filename
  • directory = “public/data”
  • # create the file path
  • path = File.join(directory, name)
  • # write the file
  • File.open(path, “wb”) { |f| f.write(upload['datafile'].read) }
    end

Checking for Dependencies

There should be a constant check done to ensure all patches and updates issued are configured and integrated properly to prevent issues. Passwords should never be stored as plain text in databases, so include some form of encryption whenever possible. Parameters and other settings that can be saved onto logfiles (for obvious activity auditing) should be screened for any that can give unwelcome access to the app.
Do check for code injections that is a common occurrence in today’s dangerous internet environment.
These and a couple more security checks should be included in your security audit to ensure you have a reasonably secure application that will surely make itself apparent should you need to look for issues in the near future.

Using the [] for conditional processing

The process shown below is and example of using the [] to have the program execute a condition that searches the contents of an array till a match is found. It goes from the top to the bottom to check each and every member of the array to look for a match doing the requested process if it is found and returning nothing if nothing is found.

class BookList
def [](key)
if key.kind_of?(integer)
return @Books[key]
else
for C in 0…@Books.length
return @Books[i] if key == @Book[i].name
end
end
return nil
end
end

Though the above shown method is quite detailed and goes through the array thoroughly, there is a n easier method which we will show with the next post making the whole process not only easier but process faster.

ルビーのシンボル、説明


イメージ提供: knowtebook.com

ルビーに関わるプログラマーの新旧関わらず、ルビーのシンボルを見ると、時々またはいつも困惑させられます。 これをスムースに行う前、慣れなくてはならないことが結構あります。 ルビー言語をについて習う人は多くの場合、ルビーオンレイルズのプロジェクトから学びます。ルビーオンレイルズではシンボルがいたるところにあります。本当にいたるところにあるのです。そのため、ルビのシンボルの概念について注意を払い、記憶することが重要になります。
ルビーのシンボルはクラスシンボルのインスタンスです。 シンボルはコロンを以下のように識別子の前につけます、":name", ":id" または":user"。 ルビーのシンボルクラスは一クラス方法を含みます。

  • all_ シンボルとインスタンスの方法、 id2name、inspect、 to_i、 to_int、 to_s、 to_sym.
  • all_symbols – ルビーのシンボルテーブルに全シンボルの配列を戻す。
  • id2name – シンボルの文字列表現を戻す、:name.id2name は “name”を戻す。
  • inspect – シンボルを文字通りに戻す。
  • to_i – 各シンボルにユニークな整数を戻す。
  • to_int – _i と同様
  • to_s - id2name と同様
  • to_sym – 特定のシンボルを一シンボルに変換する。

List of Rails Web Hosts

web_servers.jpg

Here is a list of web hosting services which support Ruby on Rails in case you might be looking for one for your website:

America:

• A2 Hosting
• AVLUX
• BILES ONLINE Ruby on Rails Hosting
• BlueHost
• Crucial Paradigm
• Dedicated Hosting
• Domain Gurus
• Hosting Rails
• JaguarPc
• SpeedyRails
• Zowes Web Hosting

Asia:

• Exaltinfo
• GVT.hk
• RailsFactory
• Web Development India
• Web Hosting Delhi

Australia:

• Anchor Systems
• Avial Web Design and Hosting
• Crucial Australia
• Hostcentral

Europe:

• 600host.net
• Blacknight Solutions
• Brightbox
• GPcom Media
• Media72 Hosting

For the complete list of Rails web hosts, visit: http://wiki.rubyonrails.org/rails/pages/RailsWebHosts

Next step : Creating the database for the filename application

The next phase or step would be to create a database for the application to use. Make sure the MySql engine is running and in the command window type “mysql -u root -p” and press enter and another enter for the password when prompted for there has not been any defined password yet. You are now logged into the engine as the root user and proceed to create the database by entering the following command “create database filename_development”. Also type in “grant all on filename_development.* to ‘ODBC’@'localhost’ this tells windows to grant access to a user named ODBC so you avoid an error when you try to access the said database from the command prompt. We next tackle the creation of tables that would allow the database to store the information we send it.

How to Install Ruby on a Shared Web Host

by Chubs

webhosting.jpg

Create a directory for all of the software you are going to download and installed. In this example, the created directory is “temp” at the root of the files system, but you can use any name and location you like.

cd /
mkdir temp
chmod 775 temp

Now, download, configure and install Ruby. You can download the latest stable source tarball for Ruby here. Wget is used in this example:

cd temp
wget ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.2.tar.gz
tar -zxvf ruby-1.8.2.tar.gz cd ruby-1.8.2

Specify an install directory since there are times when you won’t have permissions to install things in normal directories. Here, we can see the “configure” script for Ruby, creating a directory inside “/usr/local” for installation:

./configure –prefix=/usr/local/ruby –exec-prefix=/usr/local/ruby

Now, you have a Makefile that you can compile:

make
make install

Your Ruby is now installed. You should try making a symbolic link to Ruby somewhere in your local path:

ln -s /usr/local/ruby/bin/ruby /usr/local/bin/ruby

Learn Your Hashtags Manner

Hashtags popularity is definitely taking over the internet world and has become an integral part of the whole social media experience. Since twitter introduced it back in the 2008, the former ‘pound sign’ was never the same again. Now people hashtag as many as they can in any gadgets they have. Facebook also has already jumped in the bandwagon and supports hashtags. And even on other social networking sites that doesn’t support hashtags, people are still using it too. However, no matter how great we want our hashtags experience would be, some hastags are misused and can be so annoying for your friends, fans and followers. Most people who are guilty of breaking the hashtags etiquette are marketers but sometimes, even ordinary people do break the rules. Mind your manners! Here are some hastags etiquette you should bear in mind.

#donotstringtoomanywordstogether
You can put multiple words together but don’t put too much (it’s just not an unpleasant sight) and be sure to omit spaces if you do and capitalize the first letter of each new word to make it easier on the eyes.

#do #not #tag #every #word #you #think #of
I know some pictures and statuses are hard to describe in one hashtag so sometimes you have plenty of them. But please not too many hashtags in one post please. This is common in facebook where there is no character limit.

dont jump on a trend without actually relating your post to the topic
Marketers are the ones guilty of this. Just because you want to spread your brand awareness doesn’t mean you should include a tag surrounding the latest celebrity gossip.

Avoid overusing descriptive synonym tags at the end of a post
#my2cents #idea #thought are the same in meaning. So you dont need to add all of those hashtags. It seems messy and unnecessary.

Ruby Symbols: Explained


Image Source: knowtebook.com

Many programmers old and new to ruby alike, get confused when they see Ruby symbols, sometimes or all the time. It will take a lot of getting used to, before you ever do this smoothly. A lot of us came to know about the Ruby language through Ruby on Rails projects. In Ruby on Rails, symbols are everywhere. I mean everywhere. So it is important to note and memorize the concept of symbols in Ruby.
A symbol in Ruby is an instance of the class Symbol. A symbol is defined by prefixing a colon with an identifier like “:name”, “:id” or “:user”. The Symbol class in Ruby contains one class method:

  • all_symbols and instance methods id2name, inspect, to_i, to_int, to_s and to_sym.
  • all_symbols – returns an array of all the symbols in Ruby’s symbol table.
  • id2name – returns the string representation of the symbol,:name.id2name returns “name”.
  • inspect – returns the symbol literally
  • to_i – returns an integer unique for each symbol
  • to_int – same as to_i
  • to_s – same as id2name
  • to_sym – converts the symbol to a symbol.

Hashes : In Layman’s Term


Image Source: codeproject.com

One key feature of Ruby on Rails is Hashes. Now, just try to think of hashes like a PARTY. The party is a potluck occasion, naturally, each guest is required to bring an item or food. You will then need a LIST of all the guests, now this can be managed as an array. But you will also have to make a way to monitor all types of food that is being brought to the event. And to explain it further, you will want to know who is bringing the pasta, who will be bringing the fried chicken and so on. This is where Hashes come in. You will use hash to store value pairs or key pairs, this in turn will enable you to store that particular data. If for example Amanda is the “key”, and she brought cake, the variable stored and accessed by the key will be “cake”.