2011-10-17 63 views
0

我想創建一個link_to呈現:partial。我有兩個部分已存儲在視圖文件夾中,我稱之爲link_to(這是配置文件視圖)。獲取JavaScript來加載數據是下一步,但我在正確構建link_to時遇到問題。Rails 3 link_to部分結構

這裏是我有什麼:

<ul id="infoContainer"> 
    <li><%= link_to render(:partial => "about") do %>About<% end %></li> 
    <li><%= link_to render(:partial => "other") do %>Other<% end %></li> 
</ul> 

使用這些,在我show.html.erb呈現既諧音和鏈接消失。

所以我試過如下:

<ul id="infoContainer"> 
    <li><%= link_to render(:partial => 'about'), :class => "a", :remote => true do %>About<% end %></li> 
    <li><%= link_to render(:partial => 'other'), :class => "o", :remote => true do %>Other<% end %></li> 
</ul> 

兩個諧音仍顯示和「關於」 /「其他」的文字鏈接還是沒有顯示。

更新:所以我可能有link_to工作。它只是沒有渲染任何東西。需要用JavaScript來解決這個問題。

<li><%= link_to "About", (:partial => 'about'}, {:class => "a"}, :remote => true %></li> 
<li><%= link_to "Other", (:partial => 'other'}, {:class => "o"}, :remote => true %></li> 

使用上述link_to使得網址:/profiles/1?partial=about

+1

更多使用「link_to」的塊形式就像第一個argume一樣nt應該是一個URL或一個ActiveRecord對象,或一個選項散列。但是,您將'render'的結果傳遞給它,它將返回一個呈現的視圖。你想讓'render'返回的視圖成爲該鏈接的主體嗎? –

+0

不,我希望鏈接的主體只是鏈接的單詞。然後當你點擊它時,視圖應該是局部的。 – tvalent2

+0

剛剛更新了我的問題。這似乎是工作... – tvalent2

回答

2

你正在使用的link_to錯誤 - 請檢查API /文檔

,它被稱爲之一:

link_to(body, url, html_options = {}) 
    # url is a String; you can use URL helpers like 
    # posts_path 

link_to(body, url_options = {}, html_options = {}) 
    # url_options, except :confirm or :method, 
    # is passed to url_for 

link_to(options = {}, html_options = {}) do 
    # name 
end 

link_to(url, html_options = {}) do 
    # name 
end 

見:

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

也許是:

link_to syntax with rails3 (link_to_remote) and basic javascript not working in a rails3 app?

+0

我更新了我的問題。我想我右下角有'link_to'。現在我需要在JavaScript端工作來顯示/隱藏佈局。 – tvalent2

+0

你應該提出第二個問題 – Tilo

+0

當然,我計劃!謝謝。 – tvalent2

0
<ul id="infoContainer"> 
    <li><%= link_to render(:partial => 'about'), urlyouwant %></li> 
    <li><%= link_to render(:partial => 'other'), urlyouwant %></li> 
</ul> 

如果你想運行JavaScript當用戶點擊,你可以在http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-link_to_function

<ul id="infoContainer"> 
    <li><%= link_to_function render(:partial => 'about'), "alert('About!')" %></li> 
    <li><%= link_to_function render(:partial => 'other'), "alert('Other!')" %></li> 
</ul>