Posts Tagged ‘Rails’

Mephisto 0.8 (Drax) Quick Install

// March 20th, 2009 // No Comments » // Rails

If you are not a programmer, I recommend you use a different blogging engine, because even after the install is a success. You’ll still need to edit the template of your choice to get it just right. I am still not done editing this template. Installing Mephisto 0.8 (Drax) is not exactly the easiest thing to do. However, I have managed to do it twice in the last month. So here is the process to save hackers out there some time. There are a couple of requirements that you will need before proceeding with the install.

  1. Rails 2.0.2
  2. tzinfo (I have 0.3.9)
  3. At least rake 0.8.1

Installing Rails 2.0.2

This should also install rake for you. Just in case I have the command below.

sudo gem install rails -v=2.0.2

Installing tzinfo

sudo gem install tzinfo

Installing Rake

sudo gem install rake

From here change directory to the home of your rail applications.
Now run the command:

sudo wget http://github.com/technoweenie/mephisto/tarball/master.tar.gz
sudo tar -xvf technoweenie-mephisto-90e2cc253d94e2e544bc8b21f361c7360c1e9baa.tar.gz
sudo mv technoweenie-mephisto-90e2cc253d94e2e544bc8b21f361c7360c1e9baa blog

The last command simply renames the directory to something more readable. Here I renamed it to “blog”. This will be the mephisto root directory. OK! You now have all the things to need to start the installation. From here on is where I ran into all the trouble. Not to worry, I ironed out all the kinks so you should be OK!

Installation in 8 easy steps

  1. Create a database named mephisto (or one of your choosing).If you are running mysql, you can do this at the command prompt. This assumes your localhost root does not have a password.
  2. mysql -u root
    mysql> create database mephisto;
    
  3. Copy config/database.example.yml to config/database.yml
    Just copy and past the following command from the config directory.
  4. sudo mv database.example.yml database.yml
    
  5. Edit database.yml and set your database credentials.
  6. This step is important. Edit config/environment.rb
    and append the following line. This step will save you trouble when running the rake db:bootstrap command two steps down.
  7. RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION
    
  8. Time to update your boot.rb file. From your mephisto root directory, run the following command.
  9. rake rails:update
    
  10. OK! Time for the big moment! Are you ready? It’s time to bootstrap your database. Run the following command from your mephisto root directory.
  11. rake db:bootstrap RAILS_ENV=production
    
  12. If you got to this step without troubles, then consider this a big win! The next step is to deploy mephisto, I recommend mod_rails.
  13. Last step! Login to your administration at http://domain.com/admin with the username: admin and password: test

That is all there is to it. I hope everything went OK for you. Any questions or comments are welcomed.

Rails 2.0 Patrials and Collections part2

// March 19th, 2009 // No Comments » // Rails

In part one of this article we went over how to create and use partials. Part two will take what we learned from part one and see how we can use collections to make partials even more useful. We’ll see how you can set additional local variables in your partial. This article is short and assume you have read part1 of this article.

Outline for this article

  1. How do we set local variables in Rails Partials?
  2. Using Rails Partials in a loop?
  3. Is there a point to the Spacer Template in Rails Partials?

How do we set local variables in Rails Partials?
Ok, so we know that we can pass in an object to a partial with the

 :o bject

symbol. Great, but what if you want to use more variables in the partial.
This can be done using the

:locals

symbol in the render helper. For example if go back to the example given in part1

<%= render :partial => "shared/testpartial", :o bject => @name , :locals => {:testvar => @any_object_you_want } %>;

Just make sure you know the name of the local variables you are setting.

Using Rails Partials in a loop?
With partials we can format create modular views. Sometimes you’ll find the need to
render a partial many time over. On way you can do this is to run the partial in a for loop.
Rails has a shortcut for this. You can use the symbol

:collection

The symbol takes a collection of objects that the partial takes! The example from part1 took a string object. So to use the collection we can pass it a list of string objects like the following

<%= render :partial => "shared/testpartial",
:collection => %w{ Mez Adam Azmara John} %>;

This is a very useful feature, if used correctly your views show look clean. This makes maintenance
of code very manageable and enjoyable.

Is there a point to the Spacer Template in Rails Partials?
There is a option the render helper can take called

:spacer_template

It lets you specify a template that will be rendered between each of the elements in the collection.
Basically, it renders another parital. Here is how to use it.

<%= render :partial =&gt; "shared/testpartial",
:collection => %w{ Mez Adam Azmara John},
:spacer_template => "shared/another_partial" %>;

Note, this assumes you have a “_another_partial.html.erb” handy. My thinking on this is a little off. I am not sure if you really need to define another partial to display in between partials. Why not just define the content of the spacer template in the first partial? Give some good examples if you think of any!

That concludes this article. I hope you enjoyed it!

Rails 2.0 Patrials and Collections part1

// March 19th, 2009 // No Comments » // Rails

Often in web applications, the same data is displayed in more than one location. Usually this is done by copying code between different template pages. Using Rails we can avoid this and remain true to the Rails principles of “never repeating ourselves”. This is accomplished by using Rails partials.

In part one of this article we’ll see how to create and use partials. Part two will take what we learned from part one and see how we can use collections to make partials even more useful. Note, more info can be found from the book “Agile Web Development with Rails” in the chapter that deals with ActionView.

Outline for this article

  1. What the heck are Rails Partials?
  2. How do we create Rails Partials?
  3. How to use Rails Partials?

What the heck are Rails Partials?
Rails Partials are basically subroutines in their own right. You can use them many times and in many different templates. If you want, you can even pass in objects to render as parameters. A partial template looks just like a regular templates in Rails with the exception of the name of the file containing the partial. The name of the file containing the template code must start with an underscore character.

How do we create a Rails Partials?
I learn best by examples. So that’s what we’ll do here! Let take a simple example that serves no purpose but to learn from. Lets create a simple partial template that will take a string object and simply render the string. In a file named “_testpartial.html.erb”.

Some background information to keep in mind. you might be wondering where do you store this partial file? Well that depends. If you want to use this partials in views from multiple controllers, then what I like to do is create a folder called “shared” in the views directory. So go ahead and do that now. Are you done yet? Ok, lets keep moving.

Hello, <%=testpartial%>;

Keep in mind that the partial in this example expects a string object. Well now you might be wondering how does the partial know the name of the object being passed in? It uses the name of the partial as a variable name. Since this simple partial is called “_testpartial.html.erb”, we can use the variable “testpartial”. Now that we have this partial setup and created lets go and use it!

How to use Rails Partials?
Using a partial is really simple. You can do this in two places. The first way is in the controller like the following.

def index
@name = "Meseret Gebre"
render :partial =&gt; "shared/testpartial", :o bject =&gt; @name
end

The second way is in the view. For example if we setup the instance variable in the controller like the following.

def index
#var to be used in view.
@name = "Meseret Gebre"
end

Then in the view “index.rhtml” we can render the partial by placing the following code somewhere in the view.

<%=render :partial => "shared/testpartial", :o bject => @name%>;

Thats Rails partials in a nutshell. Remember in part2 of this article, we’ll look at using partials to render a collections of objects! I hope you learned something cool here and remember to never repeat yourself! Any questions or comments are welcomed!

-->

Categories