2011-07-18 22 views
0

如果我有一個模型叫客戶端和控制器,看起來像這樣如何基於使用HAML的對象創建Rails表單?

class ClientController < ApplicationController 
def new 
    @client = Client.new 
    @client.name = "New Guy" 
end 
end 

,我使用HAML,究竟會我的觀點需要的樣子呈現在此基礎上的一種形式。

我有這個

%p= @client.name 
-form_for :client, @client do |c| 
c.label :c.name 
c.text_field :c.name 

但我的HTML最終看起來像這樣

<p>New Guy</p> 
<form accept-charset="UTF-8" action="/client/new" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="orGtE1V05QIaye5kSsAi5SBdEAV0AJX9uZYUzHw5P64=" /></div> 
    c.label :c.name 
    c.text_field :c.name 
</form> 

更新: 感謝您的答案。當我改變了看法,以

%p= @client.name 
-form_for :client, @client do |c| 
    = c.label :c.name 
    = c.text_field :c.name 

我收到以下錯誤

undefined method `name' for :c:Symbol 

Extracted source (around line #3): 

1: %p= @client.name 
2: -form_for :client, @client do |c| 
3: = c.label :c.name 
4: = c.text_field :c.name 

如果我註釋掉線2,3和4名打印出罰款。我錯過了什麼?

更新2:

如果我改變視圖

%p= @client.name 
-form_for :client, @client do |c| 
c.label :name 
c.text_field :name 

沒有更多的錯誤。謝謝。

回答

0

您需要在ruby函數調用之前使用=,以便對它們進行評估。

%p= @client.name 
-form_for :client, @client do |c| 
    = c.label :c.name 
    = c.text_field :c.name 
0

我認爲這是這樣的:

%p= @client.name 
-form_for :client, @client do |c| 
    =c.label :c.name 
    =c.text_field :c.name 

這並沒有考慮到,當然任何視覺格式化......

+0

你會得到這個錯誤,因爲SO已經添加了一個額外的標籤之前-form爲:) –

+0

是的,錯過了在剪切n粘貼。我專注於'='標誌。編輯瞭解決該問題的答案。 – jaydel

0

這是你想要什麼:

%p= @client.name 

- form_for :client, @client do |c| 
= c.label :c.name 
= c.text_field :c.name 
相關問題