首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails友好Id -在id=electronics中找不到类别

Rails友好Id -在id=electronics中找不到类别
EN

Stack Overflow用户
提问于 2013-12-01 17:18:59
回答 2查看 5K关注 0票数 8

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

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

如果我去http://0.0.0.0:3000/categories/1,这是可行的。但是,当我在此页面中单击“编辑”或转到http://0.0.0.0:3000/categories/electronics ( ID为1的类别的段段式名称)时,我会收到以下错误:

代码语言:javascript
复制
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

类别模式:

代码语言:javascript
复制
class Category < ActiveRecord::Base 

    extend FriendlyId
    friendly_id :name, use: :slugged

    # Validations
    validates_uniqueness_of :name, :case_sensitive => false

end

类别主计长:

(由脚手架为测试目的而产生)

代码语言:javascript
复制
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

移徙:

(我在创建类别表之后添加了friendlyId,但我认为没有问题)

代码语言:javascript
复制
class AddColumnToCategory < ActiveRecord::Migration
  def change
    add_column :categories, :slug, :string
    add_index :categories, :slug, unique: true
  end
end

路线:

代码语言:javascript
复制
resources :categories

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-01 17:25:44

查查医生,友好的id停止了对find方法的黑客攻击(为了更大的好处),您现在必须这样做:

代码语言:javascript
复制
# Change Category.find to Category.friendly.find in your controller
Category.friendly.find(params[:id])
票数 29
EN

Stack Overflow用户

发布于 2015-04-21 09:19:22

您现在可以使用:

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

在你的模型里。

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20314927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档