2012-02-15 52 views
9

我想申請一類由button_to生成的表單中的Rails 3將類添加到生成的表單的正確button_to語法是什麼?

:class選項設置類提交按鈕所以docs告訴我們使用:form_class一類適用於形式。

E.g.

<%= button_to 'x', user_contact_path(@user, contact), :method => :delete, :form_class => "delete" %> 

這只是將屬性form_class="delete"添加到按鈕元素。我嘗試過使用:html_options等等的各種組合。

任何人都知道如何做到這一點?

回答

15

該方法對我來說工作得很好。我能夠做到:

<%= button_to "Hello", root_url, :method => :get, :form_class => "my_class" %> 

上述生成以下內容:

<form action="http://localhost:3000/" class="my_class" method="get"> 
    <div><input type="submit" value="Hello"></div> 
</form> 

然而,這是Rails的3.1作爲你的問題點的鏈接,同樣不會對Rails 3.0的工作.x自form class is hard coded

從url_helper代碼:

("<form method=\"#{form_method}\" action=\"#{html_escape(url)}\" 
    #{"data-remote=\"true\"" if remote} class=\"button_to\"><div>" + 
    method_tag + tag("input", html_options) + request_token_tag + 
    "</div></form>" 
).html_safe 
+1

感謝Syed,非常正確 - 這是硬編碼,因爲我使用3.0.7我認爲。 – digitalWestie 2012-02-15 16:28:58

+2

我實際上在今天之前不知道這一點。感謝你的回答! – 2012-02-15 19:36:16

0

嘗試

<%= button_to 'x', user_contact_path(@user, contact), {:method => :delete, :form_class => "delete"} %> 

這迫使:form_class => "delete"options散列而不是html_options哈希的一部分。

+0

HMN,這個語法並不爲我工作。必須與我的版本的軌道有關。 – digitalWestie 2012-02-15 16:26:38

相關問題