有没有办法在没有模型的情况下创建带有 formtastic 的表单? [英] Is there any way to create a form with formtastic without a model?

查看:42
本文介绍了有没有办法在没有模型的情况下创建带有 formtastic 的表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 formtastic 创建表单,但我没有与之关联的模型(带有用户名、密码和 openid URL 的登录表单).

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.

推荐答案

您可以传递一个字符串而不是一个模型,它将用于生成字段名称:

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

这将产生类似:

<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 将无法确定它应该使用哪些类型的字段,所有字段都将默认为文本字段(尽管它会自动将字段命名为 password 密码类型字段).

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

一个更好的方法是使用会话模型,看看 authlogic (http://github.com/binarylogic/authlogic) 适用于更多信息.

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.

这篇关于有没有办法在没有模型的情况下创建带有 formtastic 的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆