有没有办法用没有模型的格式创建窗体?

时间:2022-10-17 15:53:54

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

我想使用formtastic来创建表单,但是我没有与之相关的模型(使用用户名、密码和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 个解决方案

#1


44  

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

但是,您需要对您的选项进行更详细的说明,因为formtastic不能确定它应该使用什么类型的字段,所有的字段都默认为textfields(尽管它会自动生成诸如密码类型字段之类的字段)。

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.

更好的方法是使用会话模型,看看authlogic (http://github.com/binarylogic/authlogic)是如何工作的。

#1


44  

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

但是,您需要对您的选项进行更详细的说明,因为formtastic不能确定它应该使用什么类型的字段,所有的字段都默认为textfields(尽管它会自动生成诸如密码类型字段之类的字段)。

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.

更好的方法是使用会话模型,看看authlogic (http://github.com/binarylogic/authlogic)是如何工作的。