路线集合为?

时间:2012-12-05 21:27:18

标签: ruby-on-rails ruby-on-rails-3 routes ruby-on-rails-3.2

我根据此http://railscasts.com/episodes/382-tagging

为我的网站实施了标记

我想在我的房屋资源中添加标签,所以我得到这样的网址......

/房屋/ TAG1 /房屋/ TAG2 ECT

我的路线档案:

localized(['en', 'nl', 'de']) do
  scope "/:locale" do
    resources :houses do
      collection do
        get ':tag', to: 'houses#index', as: :tag
      end
    #...
    end
  end
end

众议院控制员

if params[:tag]
  @houses = House.tagged_with(params[:tag])
  @tag = Tag.find_by_name(params[:tag])
else
  @houses = House.find.all
end

众议院模特(部分)

def self.tagged_with(name)
       Tag.find_by_name!(name).houses
     end

      def self.tag_counts
        Tag.select("tags.*, count(taggings.tag_id) as count").
          joins(:taggings).group("taggings.tag_id")
      end

      def tag_list
        tags.map(&:name).join(", ")
      end

      def tag_list=(names)
        self.tags = names.split(",").map do |n|
          Tag.where(name: n.strip).first_or_create!
        end
      end

这适用于房屋资源中的标签。但是当我通过示例house / housename1进入房屋ID(显示方法)时,我收到错误消息

undefined method `houses' for nil:NilClass
house.rb:41:in `tagged_with'

我做错了什么?

Ciao..remco

1 个答案:

答案 0 :(得分:0)

房屋和标签的路线正在崩溃。您需要决定如何访问标签。我建议:

选项1

标签可以是一个单独的资源:

resources :houses
resources :tags

选项2

标记可能类似于houses#index的过滤器。在这种情况下,您无需在路径中定义标记,只需:

resources :houses

然后只需使用这种路线按标签过滤房屋:

/houses/?tag=tag1