【问题标题】:Rails - When submitting a form, then seeing the form again with errors -How to retain the values?Rails - 提交表单时,再次看到错误的表单 - 如何保留这些值?
【发布时间】:2010-11-10 05:39:29
【问题描述】:

当用户注册时,如果出现错误,提交页面后刷新,您会看到关于为什么表单没有提交到数据库的错误消息。

但是在这个表单中,原始表单中的值已经消失了,但是它们似乎在内存中,因为如果您单击刷新可以重新提交,那么这些值就在那里。

为什么 rails 不显示之前输入的值,允许用户更新重新提交?

我设计 new.html.erb 的表单

    <%= form_tag(user_registration_path, :method=>'post', :id => 'user_new') do |f| %>
.
.

        <tr>
            <td class="label">
                <%= label_tag 'password', 'Password:', :id => 'lpassword', :for => 'password' %>
            </td>
            <td class="field">
                <%= password_field_tag 'user[password]', nil, :id => 'user[password]', :maxlength => 50 %>
            </td>
            <td class="status"></td>
        </tr>

        <tr>
            <td class="label">
                <%= label_tag 'user[email]', 'Email Address:', :id => 'luser[email]', :for => 'user[email]' %>
            </td>
            <td class="field">
                <%= text_field_tag 'user[email]', nil, :id => 'user[email]', :maxlength => 150 %>
            </td>
            <td class="status"></td>
        </tr>
        <tr>
            <td class="label"><label id="lsignupsubmit" for="signupsubmit"> </label></td>
            <td class="field" colspan="2">
                <input id="signupsubmit" name="signup" type="submit" value="Sign Up" />
            </td>
        </tr>
    </table>

    <% end %>

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 devise


    【解决方案1】:

    处理这个的通常流程是这样的:

    #users_controller.rb
    def new
      @user = User.new
    end
    
    def create
      begin
        @user = User.create!(params[:user])
      rescue ActiveRecord::RecordInvalid => e
        flash[:error] = e.record.errors.full_messages.to_sentence
        render :action => "new"
      end
    end
    

    然后

    #views/users/new.html.erb
    <%= form_for(@user, :url => user_registration_path, :method=>'post', :id => 'user_new') do |f| %>
      <%= f.label :password %>
      <%= f.password_field %>
    
      <%= f.text_field :email %>
      # etc...
    <% end %>
    

    这里的区别在于使用 form_for 获取记录,并使用略有不同的 f.text_field :email 而不是 text_field_tag 帮助器,它自动将字段值设置为 @user.email。参数的数量略有不同,因为您不需要告诉这些助手字段值应该是什么,因此请查看它们的文档。

    当验证被触发时,由于渲染被调用,而不是重定向,无效的@user 对象仍然填充了最初发布的值,因此将它们插入到字段中。

    我不完全确定设计在它的内部做了什么,但这种最佳实践方法应该会让你走上正确的道路。

    一个不太干净的替代方法是从 params 散列设置字段中的值,如下所示: &lt;%= text_field_tag('user[email]', (params[:user] ? params[:user][:email] : nil), :id =&gt; 'user-email', :maxlength =&gt; 150 %&gt;。此方法还假定在发布无效表单(或在重定向中再次发送参数)时发生渲染,而不是重定向。

    【讨论】:

    • 顺便说一句,如果您打算使用 js 对这些字段做一些花哨的事情,我以前在使用诸如 :id =&gt; 'user[password]' 之类的 id 时遇到过 javascript 问题,带有方括号 @987654330 @ 在其中,所以我倾向于避免使用 user-password 或其他东西。
    • 这不适用于 吗?密码似乎没有保留该值,我认为这是一个令人沮丧的 UI。
    • 我不确定,我希望用户模型上没有实际的密码属性,而是一个加密/散列密码字段和 password= 定义为存储。尝试将attr_reader :password 添加到您的用户模型中,这可能有助于在模型在内存中时存储对密码的引用。
    • 实际上可能需要attr_accessor :password,想想看。
    猜你喜欢
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    相关资源
    最近更新 更多