link_to没有显示任何rails

时间:2014-12-07 16:23:50

标签: ruby-on-rails ruby

我正在尝试在索引页面中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。

<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

我可以从localhost手动访问文章/ new,但我不认为这是问题所在。 任何帮助表示赞赏..

1 个答案:

答案 0 :(得分:8)

下面是使用rails link_to helper打印锚标记的正确语法。你忘了'='符号。

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

&lt; %%&gt;内的Ruby代码处理在ERB模板中。

%  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