通过动态属性编辑resource_path

时间:2014-01-04 14:12:39

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

计划(对象)

    #<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

2 个答案:

答案 0 :(得分:0)

routes.rb

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

视图文件

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

答案 1 :(得分:0)

解决。

使用:子域与Rails的“子域”冲突

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