2009-05-27 71 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嗎?