我添加了列设置用户的列信息,这是由设计生成的。我不想让用户通过注册填写字段信息。我为其他页面创建了控制器,如个人资料
class PagesController < ApplicationController
def myprofile
@currentUser = User.find_by_id(current_user.id)
end
def userinfo
@currentUser = User.find_by_id(current_user.id)
if request.post?
if @currentUser.update(params[:userinfo].permit(:information))
redirect_to myprofile_path
else
render 'userinfo'
end
end
end
end
在页面上,userinfo用户应该能够编辑他的信息。
以下是观点:
<div id="page_wrapper">
<h2>Hey, <%= @currentUser.username %>, add some information about you: </h2>
<%= form_for @currentUser.information do |f| %>
<p>
<%= f.label :information %><br />
<%= f.text_area :information, autofocus: true %>
<p>
<p>
<%= f.submit "Save" %>
<p>
<% end %>
</div>
应用程序控制器:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :information, :email, :password, :password_confirmation, :remember_me) }
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :information, :email, :password, :remember_me) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :information, :email, :password, :password_confirmation, :current_password) }
end
end
当我尝试保存时,我得到了
nil的未定义方法`permit':NilClass
我该如何解决?也许有更好的方法来完成这项工作?我不想显示整个表单来编辑密码,用户名等。只有信息。
答案 0 :(得分:0)
未定义的方法`permit&#39;为零:NilClass
你做错了。您的cv::Mat
不会包含params
密钥。您的:userinfo
将如下params
。您应该将代码更改为
:user => {:information => 'value'}
当您尝试 更新 某条记录时,您需要更改
#controller
def userinfo
@currentUser = User.find_by_id(current_user.id)
if @currentUser.update(user_params)
redirect_to myprofile_path
else
render 'userinfo'
end
end
protected
def user_params
params.require(:user).permit(:information)
end
到
<%= form_for @currentUser.information do |f| %>
最后,如果您为此对应的<%= form_for @currentUser, method: put do |f| %>
设置了post
路线,则需要将其更改为controller#action