2012-06-13 34 views
2

所以,我有這樣的代碼在「應用程序/視圖/帖」的部分稱爲「_form.html.erb」Rails partials:我做錯了什麼?

<%= form_for @post do |f| %> 
    <% if @post.errors.any? %> 
     <h2>Errores:</h2> 
     <ul> 
      <% @post.errors.full_messages.each do |message| %> 
       <li><%= message %></li> 
      <% end %> 
     </ul> 
    <% end %> 
<p> 
    <%= f.label :title %><br /> 
    <%= f.text_field :title %><br /> 
<br /> 
    <%= f.label :content %><br /> 
    <%= f.text_area :content %> 
</p> 

而且我有這個代碼的兩種觀點:

「應用程序/視圖/職位/ new.html.erb」

<h1>Nuevo post</h1> 

    <%= render 'form' %> 

<p> 
    <%= f.submit "Agregar Nuevo Post" %> 
</p> 
<% end %> 

「應用程序/視圖/職位/ edit.html.erb」

<h1>Editar Post</h1> 

    <%= render 'form' %> 

<p> 
    <%= f.submit "Actualizar Post" %> 
</p> 
<% end %> 

我要的是在視圖中渲染的形式,但與代碼,我已經證明給你我得到的服務器這個錯誤輸出:

SyntaxError in Posts#new 

Showing /home/levick/rubyblog/app/views/posts/new.html.erb where line #10 raised: 

/home/levick/rubyblog/app/views/posts/new.html.erb:10: syntax error, unexpected keyword_ensure, expecting $end 
Extracted source (around line #10): 

7: </p> 
8: <% end %> 
Trace of template inclusion: app/views/posts/new.html.erb 

Rails.root: /home/levick/rubyblog 

Application Trace | Framework Trace | Full Trace 
Request 

Parameters: 

None 
Show session dump 

Show env dump 

Response 

Headers: 

None 

,與同一編輯視圖。

我的問題是:我做錯了什麼?

謝謝!

回答

1

嘗試在部分_form.html.erb

<%= form_for @post do |f| %> 
    <% if @post.errors.any? %> 
     <h2>Errores:</h2> 
     <ul> 
      <% @post.errors.full_messages.each do |message| %> 
       <li><%= message %></li> 
      <% end %> 
     </ul> 
    <% end %> 
<p> 
    <%= f.label :title %><br /> 
    <%= f.text_field :title %><br /> 
<br /> 
    <%= f.label :content %><br /> 
    <%= f.text_area :content %> 
</p> 
<%= f.submit @post.new_record? ? 'Nuevo Post' : 'Actualizar Post' %> 
<% end %> 
+0

不起作用... – Zauu

+0

您收到了什麼錯誤? –

+0

查看:http://min.us/mHje7Wu5B – Zauu

0

您錯過了<%end%>以關閉您的<%= form_for%>塊。

1

您不能在部分之外使用表單變量。在您的新頁面和編輯頁面中,您都可以重用f變量,即使它只存在於部分內部。

我建議您通過移動部分內部的提交按鈕的創建來修復錯誤。因爲您正在完成部分內部的表單,所以不要忘記在部分內部也移動<%- end %>

+0

但在我的編輯視圖按鈕說別的東西......我怎麼能保持這種行爲的以下? – Zauu

+0

您可以將變量傳遞給部分。有關示例,請參閱http://stackoverflow.com/a/3222958/136584。基本上你是設置:本地散列。 – Jason

+0

您能否給我一個恰好適合我的案例的實際例子?我嘗試了您鏈接到的問題中的示例,但沒有工作... – Zauu