4

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 :usernameUser モデルでも使用しています。

ただし、私のコントローラーでは、ログインしているユーザー (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 を明示的に呼び出す必要があるのはなぜですか? これを上書きできますか?

4

1 に答える 1

4

Friendly_id 2.2.7 でもまったく同じ問題がありましたが、Rails 2.3.5 アプリで Friendly_id 3.0.4 に更新すると、すべてが機能します。あなたが私のアプリで言及した4つの検索呼び出しをすべてテストしましたが、それらは機能します。

注意すべき点は、影響を与える可能性があるいくつかの API の変更です。私が遭遇したものは次のとおりです。

  • :strip_diacriticsに置き換えられました:strip_non_ascii

    String#to_urlオーバーライドして、代わりに Stringex に切り替えることにしましたnormalize_friendly_id

  • resource.has_better_id?今でしょ!resource.friendly_id_status.best?

  • resource.found_using_numeric_id?今でしょresource.friendly_id_status.numeric?

于 2010-05-09T13:04:40.640 に答える