學習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 –