为什么friendly-id在tabled上添加了slug列而无法找到?

时间:2017-12-02 13:38:07

标签: ruby-on-rails friendly-id

我有文章表

class CreateArticles < ActiveRecord::Migration[5.1]
def change
  create_table :articles do |t|
    t.string :title
    t.text :body

    t.timestamps
  end
end

跟随friendly_id自述文件

1)。 rails generate friendly_id

2。)将延伸添加到Article模型

 class Article < ApplicationRecord
   extend FriendlyId
   friendly_id :title, use: :slugged'
 end

3)。添加slug field to article table

class AddSlugToArticle < ActiveRecord::Migration[5.1]
  def change
    add_column :articles, :slug, :string
    add_index :articles, :slug, unique: true
  end
end

打开网络浏览器http://localhost:3000/article/new 可以看到rails terminal

enter image description here 这是文章控制器中的问题所在:

def show
  @article = Article.friendly.find(params[:id])
  @comments = @article.comments.order("created_at desc").paginate(:page => params[:page], per_page: 4)

  respond_to do |format|
    format.html
    format.json { render json: [@article, @comments], except: [:created_at, :updated_at] }
  end
end

此终端错误报告:

enter image description here

1 个答案:

答案 0 :(得分:0)

你们发现了这个错误,因为没有在强参数上添加参数

 def article_params
   params.require(:article).permit(:title, :body, :catalog_id, :slug)
 end