2014-12-07 22 views
5

我想在索引頁面中創建超鏈接,但它不顯示,也不會給出任何錯誤。以下是我的index.html.erb代碼。link_to沒有顯示任何欄杆

<h1> Listing articles </h1> 
<% link_to 'New article', new_article_path %> <---- Everything works perfectly except this doesnt show anything 
<table> 
<tr> 
<th>Title</th> 
<th>Textssss</th> 
<tr> 

<% @articles.each do |article| %> 
    <tr> 
    <td><%= article.title %> </td> 
    <td><%= article.text %> </td> 
    </tr> 
<% end %> 

</table> 

我檢查過我的路線,我認爲他們也沒關係。

Prefix Verb URI Pattern     Controller#Action 
    welcome_index GET /welcome/index(.:format)  welcome#index 
    articles  GET /articles(.:format)   articles#index 
    POST     /articles(.:format)   articles#create 
    new_article GET  /articles/new(.:format)  articles#new 
    edit_article GET  /articles/:id/edit(.:format) articles#edit 
    article  GET  /articles/:id(.:format)  articles#show 
     PATCH    /articles/:id(.:format)  articles#update 
       PUT  /articles/:id(.:format)  articles#update 
      DELETE   /articles/:id(.:format)  articles#destroy 
    root GET   /       welcome#index 

我可以手動從本地主機訪問文章/新,但我不認爲這就是問題所在。 任何幫助表示讚賞..

+3

將'='添加到'link_to'。 '<%= link_to'新文章',new_article_path%>'並閱讀了關於['ERB'](http://stackoverflow.com/questions/3952403/without-equal-in-ruby-erb-means) – 2014-12-07 16:25:44

+0

Thanks dude那清理了很多東西.. – soldiershin 2014-12-07 16:33:20

回答

8

下面是使用rails link_to helper打印錨標記的正確語法。你忘了'='符號。

<%= link_to 'New article', new_article_path %> 

ERB模板中的<%%內的Ruby代碼處理。

% enables Ruby code processing for lines beginning with % 
<% Ruby code -- inline with output %> 
<%= Ruby expression -- replace with result %> 
<%# comment -- ignored -- useful in testing %> 
% a line of Ruby code -- treated as <% line %> 
%% replaced with % if first thing on a line and % processing is used 
<%% or %%> -- replace with <% or %> respectively 
+0

謝謝我感覺很愚蠢:( – soldiershin 2014-12-07 16:28:32