0

polymorphic_path という Rails メソッドを使用しようとしていますが、間違った URL を取得しています。私のポリモーフィックな関連付けは、userable を通じて User である Student と Landlords との関係です。

ここに私のモデルがあります:

class User < ActiveRecord::Base
  belongs_to :userable, polymorphic: true
end

class Student < ActiveRecord::Base
  has_one :user, :as => :userable
end

class Landlord < ActiveRecord::Base
  has_one :user, :as => :userable
end

User オブジェクトを保持する current_user という変数があります。次の行:

<%= link_to "Profile", polymorphic_path(current_user) %>

学生/家主のURLを返す代わりに、「users/22」というURLを返します。

それが役立つ場合、これが私のroutes.rbファイルです..

resources :users
resources :students
resources :landlords

どこが間違っていますか?ありがとう!

4

2 に答える 2

0

うーん、polymorphic_path がうまく機能するかどうかわからない

# application_controller.rb    
helper_method :current_profile, :path_to_profile
def current_profile
   @current_profile ||= current_user.userable
end

def path_to_profile
   send("#{current_profile.class.downcase}_path", current_profile)
end

いくつかの追加行を使用して、表示だけでなく、他の方法でも機能するように拡張できます。

于 2013-10-13T09:33:27.777 に答える