【问题标题】:Edit resource_path by a dynamic attributes通过动态属性编辑 resource_path
【发布时间】:2014-01-22 05:01:28
【问题描述】:

程序(对象)

    #<Program id: 1, country: "UK", title: "Bee Movie", subtitle: "Born to bee wild.", 
description: "Barry B. Benson, a bee who has just graduated from ...", official_rating: 6.1,
 rating: 6.1, ptype: "movie", year: "2007", imdb: "0389790", tmdb: "5559", tvdb: nil, wiki: 
"http://en.wikipedia.org/wiki/Bee_Movie", poster: "/QhbOzpYfBOwrRiuTu9otPk919R.jpg", backdrop:
 "/eKXTOIqSFrfN8WTTxrzHMBNiUpB.jpg", trailer: "FFGaS6MDknY", popularity: 4.6948, tags: nil, 
created_at: "2013-12-29 21:47:45", updated_at: "2013-12-29 21:48:09">

程序(模型)

class Program < ActiveRecord::Base
 attr_accessor :subdomain

 def subdomain
     'something elaborated'
  end
end

Routes.rb

 scope ":subdomain" do
     resources :programs
   end

现在,如果我这样称呼它

p = Program.first
program_path(p)

我明白了

ActionController::UrlGenerationError: No route matches {:locale=>:en, :action=>"show", :controller=>"programs", :id=>nil, :format=>nil} missing required keys: [:subdomain, :id]

我在我的模型中设置了 attr_accessor。还缺少什么?

我的网址应该是:http://example.com/uk/programs/1-bee-movie

顺便说一句,我之所以叫 ':subdomain' 是因为旧的原因。因为之前是 uk.example.com,但现在,对于 seo,应该是 example.com/uk

【问题讨论】:

    标签: ruby-on-rails activerecord ruby-on-rails-4


    【解决方案1】:

    路线.rb

    scope "/(:subdomain)" do
        resources :programs
    end
    

    视图文件

    program_path(program, :subdomain => 'uk')
    

    【讨论】:

    • 我无法删除 :subdomain 因为我希望每个链接都有自己的 /uk/
    • 我必须编辑数千个部分。我正在寻找可以从 routes.rb 编辑资源的东西。这是因为“子域”是 Program 的一个属性
    【解决方案2】:

    已解决。

    using :subdomain 与 Rails 的 'subdomain' 冲突

    url program_url(p, :country_prefix => 'uk')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 2020-03-21
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多