Rails Friendly Id - Could not find Category with id = electronics

I'm trying to develop an application in Ruby on Rails 4.0 (older versions of this incredible structure have already been used), and I have some problems.

I installed the FriendlyID gem and I think that everything is in order, but I get errors when I try to test my application.

If I go to http://0.0.0.0:3000/categories/1 , this will work. But when I click on β€œedit” on this page or just go to http://0.0.0.0:3000/categories/electronics {000/categories/electronics (this is a broken category name with identifier 1), I get the following error:

 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 pointed the error end 

Category Model:

 class Category < ActiveRecord::Base extend FriendlyId friendly_id :name, use: :slugged # Validations validates_uniqueness_of :name, :case_sensitive => false end 

Controller categories:

(generated by scaffold for testing purposes)

 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 

Migration: ##

(I added friendlyId after creating the Category table, but I think everything is fine)

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

Routes:

 resources :categories 

I hope you help me. What am I doing wrong in Rails 4.0?

+8
ruby ruby-on-rails ruby-on-rails-4 friendly-id
source share
2 answers

Check the document , the friendly id stopped the hack find method (for the greater good), you now need to do:

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

Now you can use:

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

in your model.

+12
source share

All Articles