Category Archives: Rails

OK, so say you have a form as follows:


<% form_tag '/', :method => :get do -%>
<p><%= text_field_tag 'q', params[:q] ||= '' %> <%= submit_tag 'Search' %></p>
<% end %>

That form will send the contents of the text box to the page at ‘/’. Unfortunately, it will also send through the value of the submit button, which is ‘Search’ in this case. The url generated will be:

/?q=something&commit=Search

In this case, we would want only the ‘q’ parameter passing through, and in order to do this, all we need do is nullify the name parameter:


<% form_tag '/', :method => :get do -%>
<p><%= text_field_tag 'q', params[:q] ||= '' %> <%= submit_tag 'Search', :name => nil %></p>
<% end %>

This will create the following url on submission:

/?q=something

Here’s a little snippet that I use to quickly form tests to ensure that my models validation is working correctly from within unit tests:


  # In test_helper.rb
  # Give this function an array of invalid values, an array of valid values, the object and
  # attribute name to test, and it will ensure that only valid values are allowed
  def validation_tester ( valid_values, invalid_values, attribute_name, test_object )
    valid_values.each do |val|
      test_object.[]=(attribute_name, val)
      assert test_object.save, "#{attribute_name} should be valid: #{test_object.[](attribute_name)}"
      assert test_object.valid?
      assert !test_object.errors.invalid?(attribute_name)
    end

    invalid_values.each do |val|
      test_object.[]=(attribute_name, val)
      assert !test_object.save, "#{attribute_name} should NOT be valid: #{val}"
      assert !test_object.valid?
      assert test_object.errors.invalid?(attribute_name)
    end
  end

Then, use it as follows in your unit tests:


  def test_email_validation
    valid_emails = ['matt@test.com', 'matt.hall@test.com', 'matt@test.ing.com', 'matt.hall@test.ing.com']
    invalid_emails = ['@test.com', 'test.com', 'matt@', 'matt.hall@com', '.matt@test.ing.co.uk', 'matt@@test.com', '', '.@.']

    u = create_user
    validation_tester( valid_emails, invalid_emails, 'email', u)
  end

Can anyone suggest any improvements?


Creative Commons License


This work is licensed under a
Creative Commons Attribution 2.0 UK: England & Wales License.

The problem is simple; I wanted a method by which a model in my app could use a table from another MySQL Database (on the same server). The solution is equally simple:


  class MyModel &lt; ActiveRecord::Base
    set_table_name "other_db.table_name"
  end

Where other_db is the database name, and table_name is the table you want to use.

I recently had to create a cron job that controlled the delivery of newsletters to a set of subscribers. Unfortunately, the code required some refactoring, but up until it could be modified, the task still needed firing at specific intervals.

I had a little trouble finding much on this subject, so here is the code I eventually used:

./script/runner -e production "app = ActionController::Integration::Session.new; app.get 'account/send_newsletters'"

With any luck, this will help another person save a couple of minutes!

Update

Since Rails 1.2, this method of executing rails controller actions has been throwing an “uninitialized constant ActionController::Integration” exception. Adding the following line to your environments.rb file fixes this problem:

require ‘action_controller/integration’

Thanks, Dimitry Hristov!