【问题标题】:Is there anyway to make an object managed by Best_In_place also be a link?有没有办法让 Best_In_place 管理的对象也成为链接?
【发布时间】:2015-04-03 01:05:30
【问题描述】:

我正在使用Best In Place gem

这是我的对象的一个​​例子:

<h5><%= best_in_place node, :name, as: :input, activator: "#edit-node-title-#{node.id}" %> <%= link_to "<i class='fa fa-pencil'></i>".html_safe, "#", id: "edit-node-title-#{node.id}" %></h5> 

但我想做的是让node.name 属性显示为常规链接。

所以只需link_to node.name, node

如何将两者结合起来?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 best-in-place


    【解决方案1】:

    使用display_with 选项?它需要一个助手或过程。我更喜欢助手,但为简洁起见,我用 proc 显示它:

    <%= best_in_place node, :name, as: :input, 
                                   activator: "#edit-node-title-#{node.id}" 
                                   display_with: proc{|node| link_to node.name, node_path(node) }
    %> 
    <%= link_to "<i class='fa fa-pencil'></i>".html_safe, "#", id: "edit-node-title-#{node.id}" %>
    

    【讨论】:

    • 所以帮助器版本会简单地在display_with: 选项中包含帮助器方法的名称?例如,假设辅助方法是:link_to_node(node),我会在方法中只包含link_to,然后在display_with 中包含方法的名称?
    • 确实如此。您可以查看documentation 获取一些示例。
    【解决方案2】:

    您可以将Best in Place 标签包裹在link_to 标签内:

    <h5>
      <%= link_to node_path(node) do %><!-- Or whatever path you want to link_to --> 
        <%= best_in_place node, :name, as: :input, activator: "#edit-node-title-#{node.id}" %>
      <% end %>
      <%= link_to "#", id: "edit-node-title-#{node.id}" do %>
        <i class='fa fa-pencil'></i>
      <% end %>
    </h5>
    

    我还编辑了您的激活器链接,因此它也是一个块。这是未经测试的,但它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 2022-01-02
      相关资源
      最近更新 更多