Monday, February 4, 2008

Merb Monday: Environment variables, partials, etc.

I see a lot questions asked and answered in the #merb IRC room.

Many of them are questions that later I wish I remembered the answer to! So I'm going to try to keep track of questions I see asked and answered and record them here for the benefit of myself and others, and hopefully publish them here every Monday.

I might re-phrase the questions and answers slightly, as the writing tends to be quite terse on IRC.

Q: How do you check the current environment in Merb? I'm looking for the Merb equivalent to RAILS_ENV.

A: Merb.environment (Answered by: Booster)

Q: How do you render partials?

A: You use the partial method.

Scenario 1: You wish to render a partial template found in the same directory as your action's view template.

partial :foo
This will look in the current directory for a file named _foo.html.erb

Scenario 2: You wish to render a partial template found in a different directory than the action's view template.
partial 'common/foo'
This will look for the file views/common/_foo.html.erb
(Answered by: jp_n9)

Q: I'm trying to run my db:migrate and I'm getting a "no such file to load -- spec/rake/spectask" error.

A: Make sure you have the rspec gem installed.
sudo gem install rspec
(Answered by: octopod)

Q: How do I start merb in production mode?

A: Use the -e option. For example,
merb -e production
For a complete list of parameters type:
merb --help
(Answered by: Phoop)

Q: How do I get Merb to log data to the log file?

A: Make sure that you start Merb as a daemon with the -d directive, this will cause output to be logged to the log file located in the log/ directory (instead of being output to the console window).
merb -d
(Answered by: ezmobius)

Q: How can I use HAML for my templates?

A: Name your files with the .html.haml extension instead of the default .html.erb extension.

Example: name.html.haml

Second, you need to edit dependencies.rb to include the line:
dependency "haml"
You will also need to have the haml gem installed on your system. Restarting Merb will also likely be required.
(Answered by: QaDeS, bradly)

Q: Is there an easy way to get the current URL in a merb view?

A: I think it's request.uri
(Answered by: hassox)

In my testing the request variable does not appear to be directly available from a view template. However if you need that information you can easily pass the entire request variable, or just request.uri, into your view from the controller.

1 comment:

  1. Also we can get current action name ftom variable action_name