【问题标题】:link_to not displaying anything railslink_to 不显示任何东西
【发布时间】:2014-12-07 16:23:49
【问题描述】:

我正在尝试在索引页面中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的 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

我可以从本地主机手动访问文章/新文章,但我认为这不是问题。 任何帮助表示赞赏..

【问题讨论】:

  • = 添加到link_to&lt;%= link_to 'New article', new_article_path %&gt; 并阅读 ERB
  • 谢谢老兄,清理了很多东西..

标签: ruby-on-rails ruby


【解决方案1】:

使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多