[英]Rails is generating wrong path on nested resources with Polymorphic_url and STI
我有一个不那么传统的代码,我正在努力为资源生成正确的路径。
在我的routes.rb
namespace :admin do
resources :matches do
resources :lineups do
resources :substitutions
end
end
end
我的模特是:
Match.rb
class Match < ApplicationRecord
has_one :home_lineup
has_one :away_lineup
end
在home_lineup.rb上为STI结构
class HomeLineup < Lineup
end
在off_lineup.rb上为STI结构
class AwayLineup < Lineup
end
lineup.rb
class Lineup < ApplicationRecord
belongs_to :match
#has a :type column as :string
end
而且,substitution.rb
class Substitution < ApplicationRecord
belongs_to :lineup
end
当我尝试:
m = Match.last
l = m.home_lineup
s = Substitution.new
app.polymorphic_url([:admin, m, l, s])
我明白了:
NoMethodError (undefined method `admin_match_away_lineup_substitutions_url' for #<ActionDispatch::Integration::Session:0x000055f4327a60d0>)
Did you mean? admin_match_lineup_substitutions_url
admin_match_lineup_substitution_url
admin_match_lineup_substitution_path
admin_match_lineup_substitutions_path
但我真正想要的是“admin_match_lineup _...”。
我该怎么做才能解决这个问题?
我在这篇博文中找到了解决我问题的方法( http://leomayleomay.github.io/blog/2014/03/24/customize-the-polymorphic-url-for-sti/ )。
要强制阵容响应正确的名称,请将此代码添加到其文件中:
lineup.rb
def self.model_name
ActiveModel::Name.new(self, nil, "Lineup")
end
谢谢和快乐的编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.