Friendly_id ジェムを使用しています。また、ルートをネストしています。
# config/routes.rb
map.resources :users do |user|
user.resources :events
end
だから私はのようなURLを持っています/users/nfm/events/birthday-2009
。
私のモデルでは、イベント タイトルのスコープをユーザー名に限定して、 と の両方nfm
がスラッグせずにmrmagoo
イベントを持つことができるようにしたいと考えています。birthday-2009
# app/models/event.rb
def Event < ActiveRecord::Base
has_friendly_id :title, :use_slug => true, :scope => :user
belongs_to :user
...
end
has_friendly_id :username
User モデルでも使用しています。
ただし、私のコントローラーでは、ログインしているユーザー (current_user) に関連するイベントのみを引き出しています。
def EventsController < ApplicationController
def show
@event = current_user.events.find(params[:id])
end
...
end
これは機能しません。エラーが発生しますActiveRecord::RecordNotFound; expected scope but got none
。
# This works
@event = current_user.events.find(params[:id], :scope => 'nfm')
# This doesn't work, even though User has_friendly_id, so current_user.to_param _should_ return "nfm"
@event = current_user.events.find(params[:id], :scope => current_user)
# But this does work!
@event = current_user.events.find(params[:id], :scope => current_user.to_param)
SO、とにかく current_user.events に制限しているのに、なぜ :scope を明示的に指定する必要があるのですか? current_user.to_param を明示的に呼び出す必要があるのはなぜですか? これを上書きできますか?