【问题标题】:Is there any way to create a form with formtastic without a model?有没有什么方法可以在没有模型的情况下创建带有 formtastic 的表单?
【发布时间】:2010-01-02 12:26:48
【问题描述】:

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

当然我可以创建一个模型来做到这一点,但该模型只是一个没有任何有用代码的 hack。

【问题讨论】:

    标签: ruby-on-rails formtastic


    【解决方案1】:

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

    <% 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 密码类型字段)。

    更好的方法是使用会话模型,请查看 authlogic (http://github.com/binarylogic/authlogic) 的工作方式以获取更多信息。

    【讨论】:

    • 对于那些想知道为什么上述语法不适用于新版本的 Rails 的人,请使用 而不是
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 2018-12-25
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2012-07-08
    • 2017-07-16
    相关资源
    最近更新 更多