【发布时间】:2013-12-03 23:10:33
【问题描述】:
我在 Rails 4.0 中使用 friendly_id 5.0 stable 时遇到记录未找到异常
错误:
迁移:
class AddSlugToUsers < ActiveRecord::Migration
def change
add_column :users, :slug, :string
add_index :users, :slug
end
end
控制器:
class UsersController < ApplicationController
load_and_authorize_resource
before_filter :authenticate_user!
def index
authorize! :index, @user, :message => 'Not authorized as an administrator.'
@users = User.all
end
def show
#@user = User.find(params[:id])
@user = User.friendly.find(params[:id])
end
def update
authorize! :update, @user, :message => 'Not authorized as an administrator.'
@user = User.find(params[:id])
if @user.update_attributes(params[:user], :as => :admin)
redirect_to users_path, :notice => "User updated."
else
redirect_to users_path, :alert => "Unable to update user."
end
end
def destroy
authorize! :destroy, @user, :message => 'Not authorized as an administrator.'
user = User.find(params[:id])
unless user == current_user
user.destroy
redirect_to users_path, :notice => "User deleted."
else
redirect_to users_path, :notice => "Can't delete yourself."
end
end
end
数据:
INSERT INTO
users(id,email,encrypted_password,reset_password_token,reset_password_sent_at,remember_created_at,
sign_in_count,current_sign_in_at,last_sign_in_at,current_sign_in_ip,last_sign_in_ip,
created_at,updated_at,first_name,last_name,alias,bio,slug)
VALUES
(10,'me1@example.com','$2a$10$MYHASG','',null,null,0,null,null,'','',
Invalid Date,Invalid Date,'greek','god','tool','','tool');
如果我把 ID 放到 url 中就可以了
http://0.0.0.0:3000/users/10
但在使用 slug 时不起作用
http://0.0.0.0:3000/users/tool
【问题讨论】:
-
是的,尽我所能。唯一的区别是 add_index 不像教程中那样是唯一的。
-
有人知道这个错误的原因吗?,我已经按照自述文件进行操作,在多次提交后,这个错误不知从何而来。
标签: activerecord ruby-on-rails-4 friendly-id