缺少属性:slug,在Rails中使用friendly_id时

时间:2016-12-20 10:04:31

标签: ruby-on-rails friendly-id

我使用friendly_id为一系列美发沙龙生成合理的网址。

但是,我在主页上收到此错误,其中包含指向某些沙龙的链接:

ActiveModel::MissingAttributeError in Pages#home

missing attribute: slug

违规行就是这个,在home.html.haml:

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

这是整个文件:

- 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

我的测试数据库中只有3个沙龙,当我在控制台中查看它们时,它们都有slu ..我试图将他们的slu to设置为nil以重新生成slu ,,但没有结果。

我通常在salon.rb中添加这两行来设置friendly_id:

 extend FriendlyId
  friendly_id :slug_candidates, use: :slugged

我必须先得到friendly_id以某种方式生成slug吗?

我尝试过违规行的变种;例如:

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

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

1 个答案:

答案 0 :(得分:0)

原来正确的语法是:

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

这与我的情况非常接近。