Ruby on rails 构建用户配置文件页面rails
我目前正在编写代码,为每个登录到我的应用程序的用户构建一个配置文件页面。然而,我已经花了很多时间,似乎无法理解这一点。请原谅我缺乏知识,我是rails初学者,还在学习 以下是构建初始配置文件页面的用户模型的一部分:Ruby on rails 构建用户配置文件页面rails,ruby-on-rails,ruby-on-rails-3,user-profile,Ruby On Rails,Ruby On Rails 3,User Profile,我目前正在编写代码,为每个登录到我的应用程序的用户构建一个配置文件页面。然而,我已经花了很多时间,似乎无法理解这一点。请原谅我缺乏知识,我是rails初学者,还在学习 以下是构建初始配置文件页面的用户模型的一部分: has_one :profile after_create :build_profile def build_profile Profile.create(user: self) end 在my Profiles_controller.rb中 befor
has_one :profile
after_create :build_profile
def build_profile
Profile.create(user: self)
end
在my 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 %>
当我导航到profiles/28/edit时,我会收到相应的表单。但是,在保存表单时,我的数据库不会使用我提供的属性进行更新。如有任何指示、帮助或提示,将不胜感激。如果需要任何进一步的信息,请告诉我。提前谢谢你 尝试更新属性方法
def update
@profile = Profile.find(params[:id])
@profile.update_attributes(profile_params)
end
这里的问题是日志中显示的未经允许的参数
Unpermitted parameters: utf8, _method, authenticity_token, profile, commit, id
底部轮廓参数处的方法应更改为:
def profile_params
params.require(:profile).permit(:id,:name, :summary, :birthday, :user_id)
end
就这样,完成了。:-) 不走运,不过我真的很感激你的建议。如果在控制台中键入Profile.last有帮助。除id和用户id外,它将属性显示为nil。即使在控制台中单击“保存”以更新名称、生日和摘要等之后,profile=profile.last profile.update_attributes(名称:“some_name”)您会得到什么?您可以发布尝试更新时生成的日志吗?相应的日志将有助于调试issueupdate不是您正在寻找的方法,它是update_attributes谢谢大家,我将我的方法更改为update_attributes,并将permit替换为require。它现在正在工作!!!即使不需要update_属性,rails也知道您可以将其保留为update,并使用+1表示问题的方式,并提供完整的细节。
def profile_params
params.require(:profile).permit(:id,:name, :summary, :birthday, :user_id)
end