Missing attribute: slug, when using friendly_id in Rails

347 views Asked by At

I'm using friendly_id to generate sensible URLs for a chain of hairdressing salons.

However, I get this error on the home page, where I have links to some of the salons:

ActiveModel::MissingAttributeError in Pages#home

missing attribute: slug

The offending line is this one, in home.html.haml:

  = link_to salon, data: { no_turbolink: true } do

This is the whole file:

- content_for :header do
  .header-text
    %h1 Book your treatment online here
    .sub-header
      Choose a treatment from our superior quality selection
  %searchtreatment

.container-fluid.stripe-1
  .container.m-t-1
    .row
      .col-xs-12.m-t-3.m-b-1.text-center
        %h2.main-header Treatments

      .row.padding-md-up
        - @treatments.each do |treatment|
          .col-md-4.m-b-2
            %a(href="/search#?area=New England&main_category=#{treatment.gsub('&', '%26')}" data-no-turbolink='true')
              .displaybox
                = image_or_placeholder("treatments/#{treatment}.jpg")
                .title
                  = treatment

.container
  %hr

.container-fluid.stripe-2
  .container.m-t-1
    .row
      .col-xs-12.m-t-3.m-b-1.text-center
        %h2.main-header Most popular salons
      .row.padding-md-up
        - @salons.each do |salon|
          .col-md-4.m-b-3
            = link_to salon, data: { no_turbolink: true } do
              .displaybox
                = image_tag salon.picture
                .title
                  = salon.name
        %br
        %br
        %br
        %br

There are only 3 salons in my test database, and they all have slugs when I look at them in the console. I've tried to set their slugs to nil in order to regenerate the slugs, but no result.

I've set up friendly_id normally, by adding these two lines in salon.rb:

 extend FriendlyId
  friendly_id :slug_candidates, use: :slugged

Do I have to get friendly_id to generate the slug earlier in some way?

I've tried variants of the offending line; for example:

= link_to 'salon', salon_path(salon.to_param), data: { no_turbolink: true } do

and:

= link_to 'salon', salon_path(salon), data: { no_turbolink: true } do
1

There are 1 answers

0
memius On

It turned out the right syntax was:

= link_to salon_path(salon), data: { no_turbolink: true } do

which is annoyingly close to what I had.