2017-05-25 66 views
0

從edit.html.erb觀點,關於軌道參數渲染

<%= render 'form', post: @post, url: authors_post_url(@post) %> 

什麼是第二個參數後的意思是:@Post?這是命名@post發佈以用於部分_form嗎?

從_form.html.erb部分文件,

<%= form_for(post, url: url) do |f| %> 
    <% if post.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2> 
     <ul> 
     <% post.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 
    <% if @post.persisted? %> 
    <%= link_to 'Show', @post %> 
    <% end %> 
<% end %> 

如上所見,@post仍在使用這違背了命名@post的目的在渲染線後上方。

回答

1

如上所見,@post仍在使用這違背在上面的渲染線命名@postpost的目的。

正是!對某個人來說,這是潦草的編碼。該語法的目的是從其環境中抽象出部分,以便它不必依賴於可用的@post。在另一頁,部分可能呈現這樣的,例如:

<%= render 'form', post: Post.new, url: authors_posts_url) %> 

如果部分遵循的規則,只使用其本地post,它會繼續工作。但是從你的問題來看,它會打破。