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

孤街浪徒 提交于 2020-01-12 01:41:34

问题


I would like to use formtastic to create form but I don't have a model associated with that (login form with username, password and openid URL).

Of course I could create a model to do that but that model was just a hack without any useful code in it.


回答1:


You can pass a string instead of a model, which will be used to generate the field names:

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

Which will produce something like:

<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>

But you will need to be more specific with your options as formtastic won't be able to work out what types of fields it should use, all will default to textfields (although it automatically makes fields named like password password type fields).

A better way to do this though would be with a sessions model, have a look at the way authlogic (http://github.com/binarylogic/authlogic) works for more info.



来源:https://stackoverflow.com/questions/1991468/is-there-any-way-to-create-a-form-with-formtastic-without-a-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!