带有id = school_name的“不能”的友好URL错误

时间:2014-05-31 13:51:39

标签: ruby-on-rails

我正在尝试在不使用Gem的情况下实现友好的URL,

我做到了这一点;

class School < ActiveRecord::Base
  validates_uniqueness_of :permalink 

  def to_param
    permalink
  end

  def self.find_by_params(input)
    find_by_permalink(input)
  end
end

def show
    @school = School.find_by_params(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @school }
    end
  end

但我仍然得到; 无法找到ID = school_name

的学校

我注意到问题来自CanCan

我的CanCan文件看起来像这样;

class Ability
  include CanCan::Ability

  def initialize(user)
       user ||= User.new # guest user (not logged in)
       if user.role == "admin"
         can :manage, :all
       else
         can :read, :all
         can :search, :all
         can [:create, :update, :destroy], Hangout, user_id: user.id
         can [:create, :update, :destroy], SchoolEvent, user_id: user.id
         can [:create, :update, :destroy], SchoolReview, user_id: user.id
         can [:create, :update, :destroy], School, user_id: user.id
         can [:create, :update], Vote, user_id: user.id
         cannot [:create, :update, :destroy], WebEvent

       end    
 end

class SchoolsController < ApplicationController

  before_filter :authenticate_user!, except: [:index, :show, :search ]
  load_and_authorize_resource
  layout "home_layout", only: [:show]

  def show
    @school = School.find_by_params(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @school }
    end
  end
end

1 个答案:

答案 0 :(得分:0)

我只是将load_and_authorize_resource转为authorize_resource以停止使用默认的find()函数加载资源。