Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating permalinks (slugs) in Rails - why is my test failing?
    text
    copied!<p>I'm writing a small CMS as a Rails test project (also planning to use it for my personal website). I want SEO-friendly URLs so I have a test to verify that permalinks are automatically being created based on a page's title (e.g. About Us => about-us). I can't figure out why this test is failing, however. Here's the code (I'm using Rails 2.3.2):</p> <pre><code># page_test.rb # note I am using the "shoulda" framework require 'test_helper' class PageTest &lt; ActiveSupport::TestCase should_validate_presence_of :title, :permalink, :content should_validate_uniqueness_of :title should "create permalink automatically" do p = pages(:sample_page) p.save assert_equal "sample-page", p.permalink end end # pages.yml sample_page: title: Sample Page permalink: # gets automatically created by model content: This is a sample page # page.rb class Page &lt; ActiveRecord::Base validates_presence_of :title, :permalink, :content validates_uniqueness_of :title before_save :generate_permalink private def generate_permalink self.permalink = self.title.parameterize end end </code></pre> <p>What happens is that the permalink is nil, instead of "sample-page" like it's supposed to be. It works, however, if I manually put the permalink in the fixture and change the test around, for example:</p> <pre><code>p - pages(:sample_page) p.title = "Contact Us" p.save assert_equal "contact-us", p.permalink </code></pre> <p>I could fix it like this, but I'm wondering why it's not firing the before_save method for the original test.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload