我目前正在编写代码,为每个登录我的应用程序的用户构建一个配置文件页面。但是我花了很多时间,似乎无法想出这个。请原谅我缺乏知识,我是初学者,现在还在学习。
以下是构建初始个人资料页面的用户模型的一部分:
has_one :profile
after_create :build_profile
def build_profile
Profile.create(user: self)
end
在我的Profiles_controller.rb
中before_action :find_profile, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@profile = Profile
end
def show
@profile = Profile.find(profile_params)
end
def edit
@profile = Profile.find(params[:id])
end
def update
@profile = Profile.find(params[:id])
@profile.update(profile_params)
end
private
def find_profile
@profile = Profile.find(params[:id])
end
def profile_params
params.permit(:profile).permit(:name, :summary, :birthday, :user_id)
end
这是我的edit.html.erb
<%= simple_form_for @profile do |f| %>
<%= f.input :name %>
<%= f.date_select :birthday %>
<%= f.text_field :summary %>
<%= f.button :submit, 'Save', class: 'submit' %>
<% end %>
对应日志
Started GET "/profiles/28/edit" for ::1 at 2016-02-19 06:16:22 -0500
Processing by ProfilesController#edit as HTML
Parameters: {"id"=>"28"}
Profile Load (0.3ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = ? LIMIT 1 [["id", 28]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 12]]
CACHE (0.1ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = ? LIMIT 1 [["id", "28"]]
Rendered profiles/_form.html.erb (21.4ms)
Rendered profiles/edit.html.erb within layouts/application (28.8ms)
Rendered layouts/_navigation_links.html.erb (1.2ms)
Rendered layouts/_navigation.html.erb (5.2ms)
Rendered layouts/_messages.html.erb (0.1ms)
Completed 200 OK in 325ms (Views: 316.9ms | ActiveRecord: 0.7ms)
Started PATCH "/profiles/28" for ::1 at 2016-02-19 06:16:38 -0500
Processing by ProfilesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"==", "profile"=> {"name"=>"TEST", "birthday(1i)"=>"2016", "birthday(2i)"=>"2", "birthday(3i)"=>"19", "summary"=>"TEST!!!"}, "commit"=>"Save", "id"=>"28"}
Profile Load (1.1ms) SELECT "profiles".* FROM "profiles" WHERE " profiles"."id" = ? LIMIT 1 [["id", 28]]
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 12]]
CACHE (0.1ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = ? LIMIT 1 [["id", "28"]]
Unpermitted parameters: utf8, _method, authenticity_token, profile, commit, id
(0.2ms) begin transaction
(0.3ms) commit transaction
Rendered profiles/update.html.erb within layouts/application (1.4ms)
Rendered layouts/_navigation_links.html.erb (1.2ms)
Rendered layouts/_navigation.html.erb (7.2ms)
Rendered layouts/_messages.html.erb (0.1ms)
Completed 200 OK in 343ms (Views: 323.9ms | ActiveRecord: 2.5ms)
当我导航到个人资料/ 28 /编辑时,我会收到相应的表格。但是在保存表单时,我的数据库不会使用我提供的属性进行更新。任何方向,帮助或提示将不胜感激。如果需要任何进一步的信息,请告诉我。先感谢您!
答案 0 :(得分:2)
这里的问题是日志中显示的未允许的参数。
Unpermitted parameters: utf8, _method, authenticity_token, profile, commit, id
底部profile_params的方法应更改为,
def profile_params
params.require(:profile).permit(:id,:name, :summary, :birthday, :user_id)
end
多数民众赞成,完成了。 : - )
答案 1 :(得分:0)
尝试使用update_attributes方法
def update
@profile = Profile.find(params[:id])
@profile.update_attributes(profile_params)
end