我正在为糖尿病患者编写一个应用程序来计算人们需要的胰岛素。这意味着用户个人资料包括他的姓名电子邮件和密码,以及一天中不同时间的因素。数据库架构是:
ActiveRecord::Schema.define(version: 20180115094430) do
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.boolean "admin"
t.string "activation_digest"
t.boolean "activated"
t.datetime "activated_at"
t.string "reset_digest"
t.datetime "reset_sent_at"
t.float "zero_till_one"
t.float "one_till_two"
t.float "two_till_three"
t.float "three_till_four"
t.float "four_till_five"
t.float "five_till_six"
t.float "six_till_seven"
t.float "seven_till_eight"
t.float "eight_till_nine"
t.float "nine_till_ten"
t.float "ten_till_eleven"
t.float "eleven_till_twelve"
t.float "twelve_till_thirteen"
t.float "thirteen_till_fourteen"
t.float "fourteen_till_fifteen"
t.float "fifteen_till_sixteen"
t.float "sixteen_till_seventeen"
t.float "seventeen_till_eightteen"
t.float "eightteen_till_nineteen"
t.float "nineteen_till_twenty"
t.float "twenty_till_twentyone"
t.float "twentyone_till_twentytwo"
t.float "twentytwo_till_twentythree"
t.float "twentythree_till_zero"
end
end
当我创建用户时,他只是使用姓名,电子邮件和密码创建的。注册并登录后,用户可以通过RESTful routes user / 1 / edit编辑他的个人资料。我希望用户能够通过user / 1 / edit / diabsettings编辑仍为空的t.float“zero_till_one”等列,并且仍然可以在users_controller.rb中使用相同的更新操作来更新数据库: / p>
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
我怎样才能做到这一点。任何帮助高度赞赏。
最佳塞巴斯蒂安
答案 0 :(得分:1)
1-创建编辑操作
COUNT
1
1
1
0
1
0
2- edit.html.erb
def edit
@user = User.find(params[:id])
end
3 - 创建edit_diabsettings动作
<%=form_for @user, :html => { id: "edit_user", class: "your_class"} do |f|%>
<%=f.text_field :name, class: 'form-control'%>
<%=f.text_field :email,class: 'form-control'%>
<%=f.text_field :password,class: 'form-control'%>
<% content_tag :button, :type => :submit, class: 'btn btn-primary' do %>
Update User
<% end %>
<%end%>
4- in edit_diabsettings.html.erb craete form to edit name to name,email,password: -
def edit_diabsettings
@user = User.find(params[:id])
end
5-进行更新操作以更新所有字段
<%=form_for @user, :html => { id: "edit_diabsettings", class: "your_class"} do |f|%>
<%=f.text_field :zero_till_one, class: 'form-control'%>
<%=f.text_field :one_till_two,class: 'form-control'%>
<%=f.text_field :two_till_three,class: 'form-control'%>
#.... and so on that should be editable here
<% content_tag :button, :type => :submit, class: 'btn btn-primary' do %>
Update User
<% end %>
<%end%>
6-为user_params创建一个私人行动
def update
@user = User.find(params[:id])
if @user.update(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
7-因为您需要在routes.rb
中为private
def user_params
params.require(:user).permit!
end
操作创建单独的操作
edit_diabsettings