Ruby on Rails Programming Tips

Database manipulation

The database has been initialized, so now we get down to business of defining the fields in that database and include a field that we would call a foreign key which allows the establishment of the one to many relationship between the tables. This part of the process requires quite a bit of background of database creation, manipulation and handling. It also requires knowledge regarding the workings of data types. So assuming you do not have much knowledge in such areas do some more reading to give you a better understanding of what how and why they are there. The ROR developers might say that it is very easy to do and use ROR but without the background on logic formulation, data types and manipulation as well as database handling the rest of the posts which would tackle ever hardening topics and operations would be very difficult to make sense of.

Don’t get me wrong, we just want to make life easier for you and not have you not understanding anything at all.

Ensuring Ruby App Security

Experts agree on the importance of each and every app deployed using Ruby, one of the most powerful but least used platforms for building apps on the internet. Twitter is one of the best examples of the hidden talents of Ruby on which it is built upon.
On any app, there is usually a login process to which controls should be attached for logged in doesn’t mean goodie user. User authentication should be very much defined to authorization to do anything in an app so try to make this area robust with login expiry, and limiting access to changeable areas of the app.

Ruby on Rails Chases Simplicity in Programming

Ruby on Rails has focused on creating templates and designs that tackle the unglamorous problems. It is not also to create a sophisticated development framework that the engineers at Google or Amazon.com will flock to; it is like making a database modification that the great majority of Web developers face every day.

Upgrading Rails

mac_rails_on_windows.png

One tip to upgrade your Rails is by running Gem to get the latest release. You can do this using:

gem update rails -y
or
sudo gem update rails -y

If you are planning to upgrade your existing projects in Rails, you can try the following method:

• Open your_project/config/environment.rb and change RAILS_GEM_VERSION to determine the version you just upgraded to.
• Run rake rails:update from your project’s root directory (this will copy in new versions of javascript libraries, etc.).
• Check to make sure that any tests you may have written still pass.

Also remember to use gem cleanup to remove older versions of rails.

Some handy ideas for using Rails


1. Never underestimate the power of plug-ins.
It’s really going to be a waste of time developing script that somebody has already beaten you to. So instead of making your own, why not look for applications and plug-ins that do all the work for you? That’s the beauty of open source. Try looking at sites like Rails Plug-in Directory and Core Rails to help you find the particular plug-in you’re looking for.

2. Keep yourself updated.
Another beauty of open source is that there are a lot of resources, updates, fixes, reviews and all other sorts of opinions out there about Ruby and Rails and everything in-between. So try to integrate yourself into online communities that discuss Ruby and Rails programming so you can keep abreast with all the developments in the community. Trust us, you will always learn new things.

Be Part of the Rails Family: Get Involved in our Mailing Lists!

ruby.jpg

True to the ideals of web development, the Rails community is composed of people wanting to reach out to help others. This thriving community builds its foundation on the virtue of sharing knowledge with one another in order to help create a better virtual world and one of the many ways through which Ruby on Rails carry out this mission is by having mailing lists for its users. These mailing lists include: a) A general list where Rail users can discuss information and projects; b) A core list for issues concerning patches; c) A list for Script.aculo.us and Prototype talk on Javascript and Ajax, and; d) A security announcement list for critical patches.

An iPad or a Kindle Fire?

This may be an easy comparison for some with the automatic answer being “iPad, of course!” If you examine your needs well, you may find that you can live well with a Kindle Fire.


Photo Credit

Yes, the iPad is the leader in touch screen technology. Apple’s touch screen grasp is arguably the best in the market. The Apple applications again are arguably the best. There is even a Kindle for iPad application.
The main drawback to this comparison is that the iPad is very expensive, Especially the latest version. If you are someone on a budget, or if you have kids and want a gadget that they can use as well but you have to stick to a strict budget, the iPad may not be an affordable option.

The Kindle Fire boasts of a colored touch screen that can play videos, games, show books and illustrated books. It is also supposed to be easy to use and kid friendly. The Kindle fire is also at least half the price of an iPad.

If you are someone who wants a tablet from a good manufacturer but does not have a big budget or does not want to spend too much, then the Kindle may be a good choice.

Getting Started and how to open Programs : Part 2

Save the text file with the following filename “first1.rb” and we now go into the syntax checking function and the facility provided to show the syntax check verbosely. The following code shows how code is checked for syntax correctness and returns the result of the said checking by typing the following code :

$ ruby :cw first1.rb

The “-c” flag initiates Ruby’s syntax checker while the “w” flag shows the result of the syntax checker. If there is no issue with the code being checked you get a “Syntax OK” result which means you can execute the code for there are no syntax issues. Type $ruby first1.rb on the command line and you get the result ” The concatenated line is : This is my first Ruby Program “

Debugging Rails

gif_rails_debugging1.gif

You can always debug your application and get it back on the rails if ever it something goes wrong. You have to check the application log files. See to it that “tail –f” commands are running on the server.log and development.log. Rails will then show debugging and runtime information to these files and debugging information will also be displayed in the browser on requests from 127.0.0.1.

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 WeblogController < ActionController::Base
def destroy
@weblog = Weblog.find(params[:id])
@weblog.destroy
logger.info(“#{Time.now} Destroyed Weblog ID ##{@weblog.id}!”)
end
end

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