簡體   English   中英

有沒有辦法在沒有模型的情況下創建一個帶有formtastic的表單?

[英]Is there any way to create a form with formtastic without a model?

我想使用formtastic創建表單,但我沒有與之關聯的模型(使用用戶名,密碼和openid URL的登錄表單)。

當然,我可以創建一個模型來做到這一點,但該模型只是一個沒有任何有用代碼的黑客。

您可以傳遞字符串而不是模型,該模型將用於生成字段名稱:

<% semantic_form_for 'user', :url => 'login' do |f| %>
    <% f.inputs :name => 'Login Details' do %>
        <%= f.input :username %>
        <%= f.input :password %>
    <% end %>
<% end %>

這會產生類似的東西:

<form action="/login" class="formtastic user" method="post"><div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="E/NksKRd7Twh4nGp1Qc8jBQNfqYDn8gg6sWdTdCtl+g=" /></div>
    <fieldset class="inputs"><legend><span>Login Details</span></legend><ol>
        <li class="string required" id="user_username_input"><label for="user_username">Username<abbr title="required">*</abbr></label><input id="user_username" name="user[username]" size="50" type="text" /></li>
        <li class="password required" id="user_password_input"><label for="user_password">Password<abbr title="required">*</abbr></label><input id="user_password" name="user[password]" size="50" type="password" /></li>
    </ol></fieldset>
</form>

但是你需要更具體地選擇你的選項,因為formtastic將無法確定它應該使用哪些類型的字段,所有字段都默認為textfields(盡管它會自動將字段命名為password密碼類型字段)。

雖然更好的方法是使用會話模型,但請查看authlogic( http://github.com/binarylogic/authlogic )的工作方式以獲取更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM