我必须在 Alchemy CMS 页面中呈现动态内容(优惠的搜索结果)。
首先,我在 cms 后端创建了一个名为“searchresult”的页面。
我制作了一个没有精华的元素。应呈现一个表格。
seachform 具有操作 /searchresult
并且使用 (get)-search 参数调用页面:
/searchresult?utf8=✓&searchmaingroup=bb&searchprofgroup=4100
但是我可以在哪里收集这方面的数据呢?
我尝试使用 page_layout 来配置自己的 Controller 的“controller”和“action”
- name: offersearch
elements: [header, resulttable]
autogenerate: [header, resulttable]
controller: offers
action: index
cache: false
Controller :
OffersController < Alchemy::BaseController
def index
end
end
但这仍然会重定向到该 Controller ,并且搜索参数会丢失。
Alchemy cms 中是否有任何“钩子(Hook)”,我可以在其中捕获搜索参数并填充变量,例如:
@offers = Offer.where(...)
并将其带到 cms 页面元素(view-partial)
我试图在这里找到解决方案:Creating a custom Guestbook Module for Alchemy CMS
但不幸的是这对我来说还不够完整。 (我的 CMS 后端中的自定义报价模块工作正常。)
最佳答案
旁注:你看到炼金雪貂 gem 了吗? (在较旧的 Alchemy 版本中,它是内置功能)它会搜索您所有的 EssenceText
和 EssenceRichtext
(如果您不将其关闭以获取某些精华),但也许值得一看.
我不想像您在路由中设置名称那样依赖页面 URL 名称 - 因为其他内容编辑器可以轻松地重命名该页面,或者在不同的语言树中使用错误的名称创建一个页面。
相反,我会定义一个仅用于搜索结果的 page_layout。
# config/alchemy/page_layouts.yml
- name: searchresults
elements: [offer_searchresults]
unique: true
cache: false
然后您可以使用搜索处理方法扩展现有的 Alchemy::Page
类:
# app/controllers/alchemy/pages_controller_ext.rb
module Alchemy
PagesController.class_eval do
before_filter :set_searchresult_page, :only => :show
before_filter :perform_search, :only => :show
def perform_search
if params[:searchprofgroup].present? && @search_result_page
@offers = Offer.where(fb: params[:searchprofgroup])
end
end
private
def set_searchresult_page
@search_result_page = Page.published.where(page_layout: "searchresults").first
end
end
end
你要做的最后一件事就是告诉 Rails 应该使用这些扩展
# config/application.rb
config.to_prepare do
Dir.glob(Rails.root.join("app/**/*_ext*.rb")) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end
关于ruby-on-rails - 在 CMS 页面中呈现动态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21079125/