RubyonRails指导Ruby中链接创建的混乱

时间:2014-07-09 23:30:14

标签: ruby-on-rails-4

我是Rails的初学者。与Railsguide for Rails 4一起工作。

我有一个困惑:  添加链接,在某处写成:

 <h1>Hello, Rails!</h1>
   <%= link_to "My Blog", controller: "posts" %>

而某处就像

  <%= link_to 'New post', new_post_path %>

请澄清区别。

1 个答案:

答案 0 :(得分:0)

好问题。这两个都是链接助手,两者都是面向资源(有关更多信息,请查看Rails UrlHelper docs)。

第一个将呈现与特定控制器关联的链接:

<%= link_to "My Blog", controller: "posts" %>
<a href='/posts'>

第二个将呈现特定于创建新Post对象的Rails路径(这也是面向资源的)。查看Rails routing guide中的第2.3节:

<%= link_to 'New post', new_post_path %>
<a href='/posts/new'>