2009-05-27 64 views
0

如何在页面顶部确定指向它的URL后才能确定链接的位置。在本例中,我想将创建和编辑场景链接移动到页面的顶部,但正如您可以看到编辑场景取决于首先了解@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> 
+0

您的代码正在循环遍历@scens,并且仅在最后一个场景中出现“编辑场景”链接。那是对的吗? – 2009-05-27 14:15:54

回答

1

您可以将链接添加到顶部,但您需要稍后以编程方式访问它,然后将URL分配给它。这需要一些参考或查询功能,我想客户端的JavaScript,但那是因为我不知道Ruby。

或者,您可以在稍后创建链接,然后将链接放置在使用CSS定位的顶部。页面上所有DOM元素的实际位置不需要与渲染顺序相匹配。要做到这一点

0

的一种方法是使用一个辅助:

在你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 
0

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

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

我认为你的问题奠定了无效的MVC用法。难道你不认为应该在视图开始渲染之前初始化所有@member @variables吗?