Ruby on Rails: configuration Rails rake ruby setup sub directories tdd test
by bseanvt
leave a comment
Transform Matching Text with Gsub in Ruby and Regular Expression
Here is a gist that demonstrates how easy it is to transform text using #gsub and a block with Ruby.
For more helpful string extensions in Ruby check out our Ruby Gem on GitHub https://github.com/AgilionApps/rails_extensions
Rails Send_File in Production Delivers an Empty File
If you’re running Rails in production it will by default be configured to let apache or nginx send files for you. If you’re handling file downloads yourself with send_file
send_file("path/to/file.txt")
you will notice that the downloaded files are empty. To get around this just comment out the following in your config/environments/production.rb file.
# comment out for production because apache/nginx are not doing this for us #config.action_dispatch.x_sendfile_header = "X-Sendfile"
Ruby on Rails: bookmarks buffer collection content_tag excerpt helper methods loop Rails visual markup
by bseanvt
leave a comment
Simple String Concatenation of a Collection Written as a Helper for Rails
At Railsconf last week I took Greg Pollack’s online course Rails Best Practices. The interface is gorgeous and the instructions are excellent. One of the lessons involved taking a partial and moving it into a helper. I was reminded how difficult such a simple task can be. I have written about this before Yield a Block Within Rails Helper Method with Multiple Content Tags. However, that post aims to solve a slightly different problem, where the helper method takes the captured text from a block passed as an argument which essentially acts as a wrapper.
The difficulty isn’t with the logic itself and or the complexity/verbosity that the code is likely to produce. Rather, it is difficult because you have to endlessly concatenate strings and this something somewhat uncommon when programming with ruby. We have to remember that we’re working with a buffer of text about to be flushed. Here is a simple code snippet that shows how to write a helper method that loops over a collection of objects.
def bookmarks_for(user=nil)
content_tag(:div, :id => "bookmarks") do
user.bookmarks.each do |bookmark|
concat(
content_tag(:strong) { bookmark.member_name } +
excerpt(bookmark.body, '', 100)
)
end.join
end
end
Notice the use of the concat() method the “+” sugb and .join() method. All three of which bring these statements together into one final piece of html called in the view.
<%= bookmarks_for(@user) %>
Ruby on Rails: deployment Git MySQL rackspace Rails ruby ruby enterprise edition ubuntu
by bseanvt
2 comments
Installing Ruby on Rails 3, MySQL, Git, Ruby Enterprise Edition, Passenger (Mod_Rails) on Ubuntu with Rackspace Cloud.
Short and sweet. Here all the commands I run in this order to set up a brand new box. It usually takes about 10 – 15 minutes on a 256 MB RAM instance. Compiling Ruby Enterprise Edition, which is super easy, will take the most amount of time. It will seem to have gotten stuck. It hasn’t. It just takes a little while.
# Update, upgrade and install all necessary packages for Ruby on Rails server if you've got a fresh Ubuntu slice apt-get update apt-get upgrade apt-get install build-essential patch libssl-dev libreadline5-dev apt-get install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8 libopenssl-ruby imagemagick librmagick-ruby1.8 librmagick-ruby-doc libfreetype6-dev xml-core postfix # postfix will prompt you for details # use Internet Site and enter in the domain name you are planning on sending email from apt-get install apache2 apache2-prefork-dev libapr1-dev libaprutil1-dev libcurl4-openssl-dev git-core mysql-server mysql-client libmysqlclient15-dev libmysql-ruby # mysql will also prompt you to set up a root user account. set the password to be anything you like # next, download the latest release of ruby enterprise edition but when you're installing it on your own machine version numbers and release dates may have changed. # pay attention to the version and release date before the file extension. it will be something like # ... 1.8.7-2010.02 # this will change to something like 2011.03, 2011.04... etc in the future. # just double check the paths on when you are installing and make the necessary substitutions # ruby enterprise edition is available at http://www.rubyenterpriseedition.com/download.html wget http://rubyforge.org/frs/download.php/71096/ruby-enterprise-1.8.7-2010.02.tar.gz tar xzvf ruby-enterprise-1.8.7-2010.02.tar.gz ./ruby-enterprise-1.8.7-2010.02/installer # this may take a little while (just follow the instructions) # and hit enter to install in default location (recommended) when prompted # and to install passenger (which is mod_rails for apache) /opt/ruby-enterprise-1.8.7-2010.02/bin/passenger-install-apache2-module # i take the output from the above script and add it to my available modules directory vim /etc/apache2/mods-available/passenger.conf # and enter something like this in the newly created file (your version numbers will prob. be different) LoadModule passenger_module /opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/gems/1.8/gems/passenger-3.0.2/ext/apache2/mod_passenger.so PassengerRoot /opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/gems/1.8/gems/passenger-3.0.2 PassengerRuby /opt/ruby-enterprise-1.8.7-2010.02/bin/ruby # and then sym link it to the enabled directory so that apache knows about it ln -s /etc/apache2/mods-available/passenger.conf /etc/apache2/mods-enabled/passenger.conf # and now i want to include ruby enterprise edition in my path so i add it to my profile (again make sure the path is correct) vim /etc/profile.d/passenger.sh export PATH=/opt/ruby-enterprise-1.8.7-2010.02/bin:$PATH . /etc/profile.d/passenger.sh # the "." file will make the setting available for the current terminal session rails -v ruby -v rake -v # should all be working now # and which ruby # should point to the ruby enterprise edition under /opt # next i # set up public/private keys # so i can do # ssh localhost without using a password cd test -e ~/.ssh/id_dsa.pub || ssh-keygen -t dsa cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys2 # and finally install git apt-get install git-core
You should now have a server ready to server ruby on rails applications!
Front End Development Ruby on Rails: ajax disable_with javascript prototype Rails rails 3 rails helpers
by bseanvt
1 comment
Rails 3 disable_with Does Not Work with Ajax Remote Form_for
It appears that the :disable_with option on the submit_tag form helper method does not behave as expected with remote forms. I’m not sure if this is a bug or not. But the fix is pretty straight forward, but perhaps a little difficult to trouble shoot. Using submit_tag inside a remote form
<%= submit_tag "Submit", :disable_with => "Submitting...." %>
Will not work.
You have to edit your public/javascripts/rails.js file around line #167
and change
document.on("ajax:after", "form", function(event, element) {
to
document.on("ajax:complete", "form", function(event, element) {
Then things will behave as expected. This assumes you are using Prototype (rather than jQuery).
Ruby on Rails: defaults helpers onchange options_for_select Rails select select_tag to_sym
by bseanvt
2 comments
Rails Select Tag and Onchange Event Calling a Remote Function with Default Option Selected
Here is a little code snippet that will fire off a request to update_client_path when you change the select field. This stands alone, rather than being apart of a larger form. The Client::CATEGORY argument is a hash, which I populated manually in my Client.rb model. It is something like this
CATEGORY = {:lead => "Prospective clients", :past => "Previous clients..."}
In my view I have
<%= select_tag "client_category",
options_for_select(Client::CATEGORY.keys, @client.category.to_sym),
{ :onchange => remote_function( :url => update_client_path(@client), :with=>"'change_to='+this.value+''" ) } %>
One gotcha is that the select_tag, options_for_select argument expects a symbol if you’re passing in a hash as the type of the collection! It may not set the default if you provide it with a string for the second arg (which specifies which option should be set as selected).
You also need to make sure that the url update_client_path is set in your routes file and that your controller (clients_controller in this case) has the appropriate method to handle the request.
Linux ruby Ruby on Rails: console irb Linux passenger production Rails ruby enterprise edition rubygems ubuntu
by bseanvt
leave a comment
Ruby Enterprise Edition and Passenger ./script/console production fails and instead returns Loading production environment (Rails 2.3.5) Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org
After installing Ruby Enterprise Edition, REE, and Passenger on Ubuntu you may see this error message when you run script/console for the first time
./script/console production # => Loading production environment (Rails 2.3.5) Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org
You then scratch your head and run
which gem which ruby which rails
to find that all appears to be in order. You have rubygems installed , ruby is installed and so is rails. You also find that each are pointing to the correct location, which is something like /usr/bin/gem -> /opt/ruby-enterprise-x.x.x.x/bin/gem, where x.x.x.x is the version of REE.
The problem isn’t however, with any of the above. The issue is with the location of irb. If you installed (like me) irb with apt-get install irb, then irb isn’t aware of your shiny new REE and ruby gems. It’s a simple fix however, unlink irb and symlink the /usr/bin/irb to REE’s irb like so…
rm /usr/bin/irb
And symlink it to the irb that REE has in bin
ln -s /opt/ruby-enterprise-x.x.x.x/bin/irb /usr/bin/irb
Now cd into your rails app and run
./script/console production
How Beautiful is Ruby?
Working with Ruby and in particular Rails, it’s easy to take the beauty inherent in the language for granted. I mean look at this code. If you read it aloud to yourself, it reads like an english sentence that any non programmer can understand.
Forum.categories.map do |category| link_to category.name, category end.to_sentence
Forum categories map do category, link to category name, the category, end and convert to a sentence. The code returns a an english sentence as well.
Fish, French Bread, Coffee and Hamburgers with each linked to the correct resource as well!
Posts: configuration dreamhost freeze gems MySQL port Rails unpack
by bseanvt
leave a comment
Deploying to Dreamhost
Remember to include the host declaration in the database.yml file when you deploy to Dreamhost. Dreamhost does not use “localhost” which is typically the default setting when using the mysql adapter and developing locally or even on a small site.
At least for me, when I ported a Rails app to Dreamhost, this was the only “Gotcha”, because my log files were not reporting any errors and were instead serving the 500 something went wrong file.
A sample config/database.yml file
production: adapter: mysql username: youruser password: yourpasswd database: ror_production_db host: mysql.yourdomain.com
To port, I unpack my gems, if I haven’t already
rake gems:unpack
Then I freeze and package rails w/ my app just in case versions aren’t exact
rake rails:freeze:gems
Then I upload to Dreamhost!


