【问题标题】:Placing link at top将链接放在顶部
【发布时间】:2010-10-29 07:01:16
【问题描述】:

当链接指向的 URL 直到页面下方才确定时,如何将链接放置在页面顶部。在此示例中,我想将 Create 和 Edit Scenario 链接移至页面顶部,但正如您所见,Edit Scenario 取决于首先知道 @scenario_id。

<%= will_paginate @scens, :next_label => 'Older', :prev_label => 'Newer' %>
<div class="box">

<% for scenario in @scens %>
    <% @created = scenario.created_at %>
    <% @updated = scenario.updated_at %>
    <% @scenario_id = scenario.id %>
    <% if scenario.scenario_image.exists? %> 
        <%= scenario_image_tag(scenario) %>
    <% end %>
  <%= simple_format(scenario.description) %>
<% end %>
</div>

<% if session[:role_kind] == "controller" %>
    <p>
    <%= button_to "Create new scenario", :action => "create" %>
    <% if @scens.size > 0 %>
        <%= button_to "Edit scenario", :action => "edit", :id => @scenario_id %>
    <% end %>
    </p>

【问题讨论】:

  • 您的代码在@scens 上循环,只有最后一个场景有一个“编辑场景”链接。对吗?
  • 正在使用分页库。所以每个网页只显示一个场景。变量scenario是当前显示的场景。

标签: html ruby-on-rails hyperlink


【解决方案1】:

您可以在顶部添加链接,但您需要稍后以编程方式访问它,然后将 URL 分配给它。这需要某种引用或查找功能,我在考虑客户端 javascript,但那是因为我不了解 Ruby。

或者,您可以稍后在获得 URL 后创建链接,并使用 CSS 定位将链接放置在顶部。页面上所有 DOM 元素的实际位置不必与它们呈现的顺序相匹配。

【讨论】:

    【解决方案2】:

    一种方法是使用帮助器:

    在您的 helper.rb 文件中:

    def stack_example(scens, &block)
      html = 'Scenario Details'
      edit_link = 'Edit Link'
      yield html, edit_link
    end
    

    那么在你的部分中,你可能会有类似的东西:

    <% stack_example(@scens) do |html, edit_link| %>
      <%= edit_link %><br>
      <%= html %>
    <% end %>
    

    应该输出以下内容:

    Edit Link
    Scenario Details
    

    【讨论】:

      【解决方案3】:

      我不明白。为什么要在视图层创建模型?为什么不在控制器中创建模型变量?某样东西:

      class your_controller
        def your_method
          @scenario_id = ...
        end
      end
      

      我认为您的问题在于无效的 MVC 使用。你不认为所有的@member @variables 都应该在视图开始渲染之前初始化吗?

      【讨论】:

        猜你喜欢
        • 2018-07-29
        • 2021-06-01
        • 2012-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多