【问题标题】:Rails Friendly Id - Couldn't find Category with id=electronicsRails 友好 ID - 找不到 id=electronics 的类别
【发布时间】:2013-12-01 17:18:59
【问题描述】:

我正在尝试在 Ruby on Rails 4.0 中开发一个应用程序(已经使用过这个令人难以置信的框架的旧版本),但遇到了一些麻烦。

我安装了FriendlyID gem,我认为一切正常,但是当我尝试测试我的应用程序时收到错误消息。

如果我去http://0.0.0.0:3000/categories/1,这行得通。但是当我点击这个页面中的“编辑”,或者只是去http://0.0.0.0:3000/categories/electronics(这是ID为1的类别的slugged名称)时,我收到以下错误:

Couldn't find Category with id=electronics
# Use callbacks to share common setup or constraints between actions.
def set_category
   @category = Category.find(params[:id])  #Here's pointed the error
end

类别型号:

class Category < ActiveRecord::Base 

    extend FriendlyId
    friendly_id :name, use: :slugged

    # Validations
    validates_uniqueness_of :name, :case_sensitive => false

end

类别控制器:

(由脚手架生成用于测试目的)

class CategoriesController < ApplicationController
  before_action :set_category, only: [:show, :edit, :update, :destroy]

  # GET /categories
  # GET /categories.json
  def index
    @categories = Category.all
  end

  # GET /categories/1
  # GET /categories/1.json
  def show
  end

  # GET /categories/new
  def new
    @category = Category.new
  end

  # GET /categories/1/edit
  def edit
  end

  # POST /categories
  # POST /categories.json
  def create
    @category = Category.new(category_params)

    respond_to do |format|
      if @category.save
        format.html { redirect_to @category, notice: 'Category was successfully created.' }
        format.json { render action: 'show', status: :created, location: @category }
      else
        format.html { render action: 'new' }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /categories/1
  # PATCH/PUT /categories/1.json
  def update
    respond_to do |format|
      if @category.update(category_params)
        format.html { redirect_to @category, notice: 'Category was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /categories/1
  # DELETE /categories/1.json
  def destroy
    @category.destroy
    respond_to do |format|
      format.html { redirect_to categories_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_category
      @category = Category.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def category_params
      params.require(:category).permit(:name)
    end
end

迁移:##

(我在创建Category表后添加了friendlyId,但我认为还可以)

class AddColumnToCategory < ActiveRecord::Migration
  def change
    add_column :categories, :slug, :string
    add_index :categories, :slug, unique: true
  end
end

路线:

resources :categories

希望你能帮助我。 我在 Rails 4.0 中做错了什么?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 friendly-id


    【解决方案1】:

    Check the doc,友好的 id 停止破解 find 方法(为了更大的利益),你现在必须这样做:

    # Change Category.find to Category.friendly.find in your controller
    Category.friendly.find(params[:id])
    

    【讨论】:

    • 我必须更改控制器中的每个 .find() 还是仅更改 set_category 中的?另一件事,究竟什么是“set_category”,你能发布一个指向这个“东西”的参考链接吗?
    • 每一个find都与一个友好的ID相关
    • 不是,这是脚手架生成的,不知道为什么会存在这个方法
    • 这个行得通,谢谢(:(但还是不知道为什么这个方法存在)
    • 哦,好吧,这只是避免重复的便捷方法
    【解决方案2】:

    您现在可以使用:

    extend FriendlyId friendly_id :name, use: [:finders]

    在你的模型中。

    【讨论】:

    • 谢谢,这是一个比接受的答案更好的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 2013-09-06
    • 2022-09-28
    • 2016-04-25
    • 1970-01-01
    • 2016-05-15
    • 2015-06-11
    相关资源
    最近更新 更多