2012-07-16 90 views
0

我想使鏈接標記像這樣任何人都可以告訴我如何在Rails3中做鏈接標記嗎?

用戶列表/用戶A /博客

這可能是這樣也用戶列表/用戶A /檔案

用戶列表可以模型。
用戶A將被參數
的博客或個人資料被賦予將是一個動作(方法)

如何傳遞這些信息,從我的控制器,查看是否看法是這樣的。 你明明可以修改!謝謝

  <ul class="breadcrumb"> 
      <li> 

      <% if [email protected]? %> 
      <%= @FirstDirPath %> <span class="divider">/</span> 
      <% end %> 

      <% if [email protected]? %> 
      <%= @SecondDirPath %> <span class="divider">/</span> 
      <% end %> 

      <% if [email protected]? %> 
      <%= @ThirdDirPath %> <span class="divider">/</span> 
      <% end %> 

      <% if [email protected]? %> 
      <%= @FourthDirPath %> <span class="divider">/</span> 
      <% end %>   

      </li> 
      </ul> 

回答

1

在您的意見中使用link_to助手。例如:

<%= link_to "My link", "link_url" %> 

爲了您的AR記錄,你可以使用這個語法:

<%= link_to "Profile", user %> 
<%= link_to article.title, article %> 
# ... etc. 

UPD

我不明白你想要做什麼。在視圖中,您可以使用在動作中定義的實例變量。例如:

@links = [ 
    { label: "My link",  destination: some_path }, 
    { label: "Another link", destination: "path/path" }, 
    { label: "Third link", destination: third_list_path } 
] 

比的觀點,您可以:

<% @links.each do |link| %> 
    <%= link_to link[:label], link[:destination] %> 
<% end %> 
+0

謝謝!我知道如何使用link_to。我想知道的是如何使它變化。我在我的問題中編寫的代碼是用application.html.erb編寫的。所以每次都會根據控制器,操作方法和參數進行更改。所以這些信息應該從控制器過去。如何讓我的觀點靈活適應這種情況? – MKK 2012-07-16 16:32:50

+0

您可以通過params [:controller]和params [:action]獲得控制器和操作。然後依賴於控制器和動作,你可以在你的佈局中使用特殊的部分: <%=渲染部分:「#{params [:action]} _ my_partial_name」%> – 2012-07-16 18:32:15

+0

感謝您的回覆。但是如果我想爲該變量鏈接標籤添加標籤和目標鏈接路徑呢?我怎麼能通過他們從控制器查看? – MKK 2012-07-16 18:47:34

相關問題