我正在开发一个应用程序,用户可以在其中编辑用户,并在提交后将其重定向到他们的展示个人资料视图。
这一切似乎都按照计划进行,但由于某些原因,当我回到编辑视图时,我看到我的一些表单字段会自动预先填充,而其他人则不会。
为什么会这样?
具体而言,所有文本字段都被记住并预先填充,但我的图像文件字段和时间每周字段不是。它们肯定仍在数据库中并显示在我的节目视图中但未在编辑视图中预先填充?
某些类型的字段是否未预填充或此行为是什么?理想情况下,我希望预先填充所有字段(图像,日期,文本等)
这是我的代码:
修改视图:
<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
Scheema:
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
答案 0 :(得分:3)
“预先填充”有两个含义,您需要考虑这两个含义:
- 基于浏览器的数据
- 基于服务器的数据
醇>
<强>浏览器强>
这里的不同之处在于,基于浏览器的数据基本上是您在电子商务网站上输入表格的“记住我”的内容。
我提到这个的原因是因为当你有一个用户表单时,现代浏览器(exclusing 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,但我们制作了图像表单以显示图像,然后您可以上传新的图像。
-
<强>时间强>
坦率地说,我不确定你的时间。与上面的解释一样,您需要确保使用数据库中的属性来填充时间字段。我看到你使用了很多不同的复选框,虽然可能有助于创建一个更好的系统,但可能不会填充你想要的数据