学习Ruby和Rails。在下面的入门指南,如果我调用什么是Ruby立场的edit_post_path
rails generate scaffold Post name:string title:string content:text
它产生除其他事项外码就像index.html.erb如下:
<% @posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
我在上面只关注edit_post_path
,和我问题是,它是什么 - 特别是从Ruby的立场。它肯定具有Ruby方法的每一个外观,它嵌入在其他代码中,绝对是Ruby:posts.each do |post|...end
这就是所有的Ruby
但是,如果edit_post_path是一个Ruby方法,它的代码在哪里? 'post'是我提供给Rails的一个标签,所以大概这个Ruby方法应该在我的站点目录中的某处以及上面调用“rails generate scaffold ...”时生成的其他Ruby代码(即它不会在例如Rails特定的目录)。但是在任何地方都没有定义这种方法'edit_post_path'。那么它是否真的不是Ruby,只是出于某种原因而设计的那种方式,实际上只是一些由严格专有的Rails处理的文本。这是Rails的一个很酷的例子吗?
打败我吧,很好的答案。 OP,如果你想查看helper方法的位置(是的,它们是“直接Ruby方法”,它们只是在你的应用程序启动时动态定义),请参阅本节:https:// github。 COM /导轨/导轨/ BLOB/d761ec486416df673030417047671c1a190f5aba/ActionPack的/ lib目录/ action_dispatch /路由/ route_set.rb#L143-204 –