Ruby on rails RubyonRails:动态多路径到一个页面
在Rails 4中,我有3个模型Ruby on rails RubyonRails:动态多路径到一个页面,ruby-on-rails,ruby,ruby-on-rails-4,friendly-id,Ruby On Rails,Ruby,Ruby On Rails 4,Friendly Id,在Rails 4中,我有3个模型 class Tag < ActiveRecord::Base # attr_accessible :id, :name end class Category < ActiveRecord::Base # attr_accessible :id, :name end class Product < ActiveRecord::Base # attr_accessible :id, :name belongs_to :tag
class Tag < ActiveRecord::Base
# attr_accessible :id, :name
end
class Category < ActiveRecord::Base
# attr_accessible :id, :name
end
class Product < ActiveRecord::Base
# attr_accessible :id, :name
belongs_to :tag
belongs_to :category
delegate :tag_name, to: :tag
delegate :category_name, to: :category
end
但不知道如何自动添加它。(我使用friendly\u id
gem使URL变得更友好)
这里是我用来匹配路线的路径,但它不是我想要的动态路径。当要求不仅是标签
和类别
,而且是子类别
,超级类别
。。。routes.rb
将快速增加,并且不会DRY
有人能给我另一个建议吗?以下是你想要的丑陋解决方案:
#routes.rb
match "(:first_id)/(:second_id)/(:third_id)", to: "home#index", via: :get
正如您提到的,您需要最后一个参数,这将是产品id。所以我们只需要得到最后一个参数
#home controller
def index
product_id = case
when params[:third_id].present?
params[:third_id]
when params[:second_id].present?
params[:second_id]
when params[:first_id].present?
params[:first_id]
end
@product = Product.find(product_id)
end
#home controller
def index
product_id = case
when params[:third_id].present?
params[:third_id]
when params[:second_id].present?
params[:second_id]
when params[:first_id].present?
params[:first_id]
end
@product = Product.find(product_id)
end