【问题标题】:How to implement this nested linked_to_function?如何实现这个嵌套的linked_to_function?
【发布时间】:2023-03-15 17:06:01
【问题描述】:

在我看来,我希望用户能够浏览产品目录。层次结构如下:

Category
  Product
    AdditionalInfoForm

在我的主视图中,我为类别渲染了部分内容:

#opContent
  = render :partial => 'op_main_categories'

op_main_categories 部分定义如下:

- @controller.getPortalCategories.each do |c|
  = link_to_function "#{c.name}", :class => 'opCategoryHeaderText' do |page| 
    - partial = escape_javascript(render :partial => 'op_secondary_categories', :locals => { :c => c })
    - page << "$('opContent').update(\"#{partial}\")"

当用户点击上述部分中的链接时,产品通过 op_secondary_categories 部分呈现:

 - @controller.getPortalProductsForCategory(c).each do |p|
   = link_to_function "#{p.name}", :class => 'opCategoryHeaderText' do |page| 
     - partial = escape_javascript(render :partial => 'op_did_line', :locals => { :p => p })
     - page << "$('opContent').update(\"#{partial}\")"

op_did_line 现在是纯文本:

%p Yes, this is form! Hello?

此设置将生成以下链接(针对一个类别):

<a class="opCategoryHeaderText" onclick="$('opContent').update("<a class=\"opCategoryHeaderText\" href=\"#\" onclick=\"$(\'opContent\').update("<p>Yes, this is form! Hello?</p>\n"); return false;\">IPSDN + PVC 3yr<\/a>\n<a class=\"opCategoryHeaderText\" href=\"#\" onclick=\"$(\'opContent\').update("<p>Yes, this is form! Hello?</p>\n"); return false;\">PVC<\/a>\n"); return false;" href="#">PVC</a>

添加第二层 link_to_function 会破坏所有代码。我不再能够浏览层次结构。当只有一个 link_to_function 存在时,我可以浏览。

该项目仍在使用 Rails 2.3。有什么更好的方法来实现这一点?

【问题讨论】:

  • 这是实际代码的剪切和粘贴吗?因为我看到语法错误。发布实际代码会有所帮助。以及发布你得到的错误。
  • .replaceWidth() 不是 PrototypeJS 方法 - 它应该是 .replace()

标签: ruby-on-rails prototypejs partial-views link-to rjs


【解决方案1】:

我的问题通过“Jose”在page.insert_html does not insert html with link_to_function (rails) 中的答案解决了

答案中提供的代码放在 rails/lib/rails_ext.rb

【讨论】:

    猜你喜欢
    • 2017-04-01
    • 2011-05-06
    • 2023-03-29
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多