【问题标题】:How come some of my user fields get remember & pre-populated and others don't?为什么我的一些用户字段会被记住并预先填充而其他人却没有?
【发布时间】:2014-09-06 23:57:10
【问题描述】:

我正在开发一个应用程序,人们可以在其中编辑他们的用户,并在提交后将他们重定向到他们的节目资料视图。

这一切似乎都按计划进行,但由于某种原因,当我返回编辑视图时,我看到我的一些表单字段会自动预填充,而另一些则不会。

为什么会这样?

特别是所有文本字段都被记住并预先填充,但我的图像文件字段和每周时间字段没有。它们肯定还在数据库中并显示在我的显示视图中,但没有预先填充在编辑视图中?

某些类型的字段是否未预先填充,或者这是什么行为?理想情况下,我希望预先填充所有字段(图像、日期、文本等)

这是我的代码:

编辑视图:

<div class="editprofilebox">

    <h1>Take a moment to fill out your profile:</h1>

    <div class="container-fluid">


            <div class="edit-profile-form">
                <%= form_for @user, :html => { :role => 'form', :class => 'form-horizontal', :multipart => true } do |f| %>

                    <div class="form-inputs">
                    <div class="row">


                        <div class= "col-sm-4">

                            <div class: "form-group">
                            <%= f.text_field :first_name, class: "form-control", placeholder:"First Name" %>
                            </div>

                            <div class: "form-group">
                            <%= f.text_field :last_name, class: "form-control", placeholder:"Last Name" %>
                            </div>

                            <div class: "form-group">
                            <%= f.label :profile_image, class: "control-label" %>
                            <%= f.file_field :image, class: "profile-picture-upload" %>
                            </div>

                        </div>



                        <div class= "col-sm-4">
                            <div class: "form-group">
                            <%= f.label :twitter %>
                            <%= f.text_field :twitter, class: "form-control", placeholder:"Type your update title here" %>
                            </div>

                            <div class: "form-group">
                            <%= f.text_field :occupation, class: "form-control", placeholder:"Occupation" %>
                            </div>

                            <div class: "form-group">
                            <%= f.text_field :gender, class: "form-control", placeholder:"Gender" %>
                            </div>

                            <div class: "form-group">
                            <%= f.text_field :work_history, class: "form-control", placeholder:"Work History" %>
                            </div>

                            <div class: "form-group">
                            <%= f.number_field :years_of_experience, class: "form-control", placeholder:"years_of_experience" %>
                            </div>
                        </div>

                                    <h3> time available </h3>

                        <div class="col-sm-2">

                                            <%= f.label :Monday %>
                                            <%= f.check_box :monday, class: "time-checkbox", placeholder:"Type your update title here" %>
                                            <%= f.time_field :mondaytime1, class: "form-control time-box"%>
                                            <%= f.time_field :mondaytime2, class: "form-control time-box" %>
                                            <%= f.label :Tuesday %>
                                            <%= f.check_box :tuesday, class: "time-checkbox", placeholder:"Type your update title here" %>
                                            <%= f.time_field :tuesdaytime1, class: "form-control time-box" %>
                                            <%= f.time_field :tuesdaytime2, class: "form-control time-box" %>
                                            <%= f.label :Wednesday %>
                                            <%= f.check_box :wednesday, class: "time-checkbox" %>
                                            <%= f.time_field :wednesdaytime1, class: "form-control time-box" %>
                                            <%= f.time_field :wednesdaytime2, class: "form-control time-box" %>
                                            <%= f.label :Thursday %>
                                            <%= f.check_box :thursday, class: "time-checkbox" %>
                                            <%= f.time_field :thursdaytime1, class: "form-control time-box" %>
                                            <%= f.time_field :thursdaytime2, class: "form-control time-box" %>

                        </div>
                        <div class:"col-sm-2">
                                            <%= f.label :Friday %>
                                            <%= f.check_box :friday, class: "time-checkbox" %>
                                            <%= f.time_field :fridaytime1, class: "form-control time-box" %>
                                            <%= f.time_field :fridaytime2, class: "form-control time-box" %>
                                            <%= f.label :Saturday %>
                                            <%= f.check_box :saturday, class: "time-checkbox" %>
                                            <%= f.time_field :saturdaytime1, class: "form-control time-box" %>
                                            <%= f.time_field :saturdaytime2, class: "form-control time-box" %>
                                            <%= f.label :Sunday %>
                                            <%= f.check_box :sunday, class: "time-checkbox" %>
                                            <%= f.time_field :sundaytime1, class: "form-control time-box" %>
                                            <%= f.time_field :sundaytime2, class: "form-control time-box" %>

                        </div>

                    </div>
                    </div>

                    <div class="form-actions">
                      <%= f.button :submit, class: "btn btn-default btn-lg" %>
                    </div>

                <% end %>
            </div>


        </div>

</div>

显示视图:

<div class="profilebox">

    <div class="profile-title-box" >
        <%= @user.first_name %> <%= @user.last_name %> profile
    </div>
    <hr>

<div class="profile-container">

    <div class="row ">

        <div class="profile-image-box col-sm-4" >
            <%= image_tag @user.image.thumb('150x185#').url if @user.image_stored? %>
        </div>

        <div class="profile-info-box col-sm-8">
            <%= @user.email %>
            </br>
            <%= @user.first_name %>
            </br>
            <%= @user.last_name %>
            </br>
            <%= @user.occupation %>
            </br>
            <%= @user.gender %>
            </br>
            <%= @user.work_history %>
            </br>
            <%= @user.years_of_experience %>
            </br>
        </div>

    </div>

</div>

<hr>

        <div class="profile-extra-box">
            <h3>Complete your profile here:</h3>
            <%= link_to 'Edit', edit_user_path(@user) %>
        </div>


</div>

用户控制器:

class UsersController < ApplicationController

    def new
    end

    def show
        @user = User.find(params[:id])
    end

    def index
    end

    def edit
        @user = User.find(params[:id])
    end

    def update
        @user = User.find_by_id(params[:id])
        if @user.update_attributes(user_params)
            flash[:success] = "User updated successfully!"
            redirect_to user_path
        else 
            flash[:danger] = "User could not be updated!"
        end
    end

    def destroy
    end


    def user_params
      params.require(:user).permit(:first_name, :last_name, :email, :password, :image, :twitter, :monday, :mondaytime1, :mondaytime2, :tuesday, :tuesdaytime1, :tuesdaytime2, :wednesday, :wednesdaytime1, :wednesdaytime2, :thursday, :thursdaytime1, :thursdaytime2, :friday, :fridaytime1, :fridaytime2, :saturday, :saturdaytime1, :saturdaytime2, :sunday, :sundaytime1, :sundaytime2, :occupation, :gender, :years_of_experience, :work_history)
    end


end

示意图:

ActiveRecord::Schema.define(version: 20140906225655) do

  create_table "users", force: true do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "first_name"
    t.string   "last_name"
    t.string   "image_uid"
    t.string   "image_name"
    t.text     "twitter"
    t.boolean  "monday"
    t.text     "mondaytime1"
    t.text     "mondaytime2"
    t.boolean  "tuesday"
    t.text     "tuesdaytime1"
    t.text     "tuesdaytime2"
    t.boolean  "wednesday"
    t.text     "wednesdaytime1"
    t.text     "wednesdaytime2"
    t.boolean  "thursday"
    t.text     "thursdaytime1"
    t.text     "thursdaytime2"
    t.boolean  "friday"
    t.text     "fridaytime1"
    t.text     "fridaytime2"
    t.boolean  "saturday"
    t.text     "saturdaytime1"
    t.text     "saturdaytime2"
    t.boolean  "sunday"
    t.text     "sundaytime1"
    t.text     "sundaytime2"
    t.string   "occupation"
    t.string   "gender"
    t.string   "work_history"
    t.decimal  "years_of_experience"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

【问题讨论】:

  • 我认为你应该转储一些格式更差的代码,这样更难阅读。

标签: ruby-on-rails forms ruby-on-rails-4


【解决方案1】:

“预人口”有两个内涵,两者都需要考虑:

  1. 基于浏览器的数据
  2. 基于服务器的数据

浏览器

这里的区别在于,基于浏览器的数据基本上是您在电子商务网站等的表单中键入的“记住我”的内容。

我之所以提到这一点是因为当你有一个用户表单时,现代浏览器(不包括IE)通常会用你以前使用过的相关数据填充它。这可以通过 Chrome 网站上的 introduction to Autofill 看到:

本质上,这意味着如果您在网页上加载标准的“输入名称”,Chrome 会尽力使用您保存或输入到其他网站的数据填充它们。

首先,您需要确保浏览器没有输入您的详细信息。如果是这种情况,则意味着无论如何您都必须使服务器端功能正常工作。

无论哪种方式,您都应该考虑使用基于服务器的数据,如下所述:


服务器

其次,您将拥有基于服务器的数据。这是真实的 Rails 数据,以及您在页面中需要的内容:

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   def edit
      @user = User.find params[:id]
   end
end

#app/views/users/edit.html.erb
<%= form_for @user do |f| %>
   <%= f.text_field :first_name %>
   <%= f.text_field :last_name %>

   <%= image_tag @user.image.url if @user.image.present? %>
   <%= f.file_field :image %>
   <%= f.submit %>
<% end %>

根据form_for documentation,如果您填充了正确的 ActiveRecord 对象,并且数据库中有数据,则调用基于属性的输入助手应该为您填充。

一些注意事项,但是:

--

图片

使用file_field不会预填充您的图像。

file upload 元素与 image 元素明显不同 - 只是上传元素无法向您显示图像。这意味着您必须在 edit 表单中明确显示图像,如果您确实想显示它:

# Edit View
<%= image_tag @user.image.thumb('150x185#').url if @user.image_stored? %>
<%= f.file_field :image %>

我们用过这个方法here(只需免费注册并尝试上传个人资料图片):

虽然我们在这里大量使用了 JQuery,但我们还是让图像表单显示图像,然后您可以上传新的图像。

--

时间

坦率地说,我不确定您的时间字段。

与上面的解释一样,您需要确保使用数据库中的属性来填充时间字段。我看到您使用了很多不同的复选框,虽然可能有助于创建更好的系统,但可能不会填充您想要的数据

【讨论】:

  • 非常感谢!这是一个令人难以置信的深入回答。我不确定复选框的属性,似乎它们的行为类似于文件字段并且不记得数据。如果不使用预先填充服务器端数据的复选框,您将如何实施每周可用性计划?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-09
  • 2016-06-22
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 2017-01-21
相关资源
最近更新 更多