[英]How can I get correct infromation with params?
I added column information to model User, that was generated by devise. 我将列信息添加到模型User中,该信息是由devise生成的。 I don't want to make user to fill field Information by registration. 我不想让用户通过注册来填写字段信息。 I created controller for other pages like profile 我为个人资料等其他页面创建了控制器
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
On page userinfo user should be able to edit his information. 页面上的userinfo用户应该能够编辑他的信息。
Here is view: 这是视图:
<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>
Application Controller: 应用控制器:
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
When I try to save it, I get 当我尝试保存它时,我得到
undefined method `permit' for nil:NilClass nil:NilClass的未定义方法“ permit”
How can I fix it? 我该如何解决? Maybe there is better way to do this job? 也许有更好的方法来完成这项工作? I don't want to show whole form to edit password, username etc. Only information. 我不想显示整个表单来编辑密码,用户名等。仅提供信息。
undefined method `permit' for nil:NilClass nil:NilClass的未定义方法“ permit”
You are doing it wrong. 你做错了。 Your params
will not contain :userinfo
key. 您的params
将不包含:userinfo
键。 Your params
will look like this :user => {:information => 'value'}
. 您的params
将如下所示:user => {:information => 'value'}
。 You should change your code to below 您应该将代码更改为以下
#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
And also as you are trying to update a certain record, you need to change 而且,当您尝试更新某些记录时,您需要更改
<%= form_for @currentUser.information do |f| %>
to 至
<%= form_for @currentUser, method: put do |f| %>
Finally if you have set a post
route for this corresponding controller#action
, then you need to change it to put
最后,如果您为此相应的controller#action
设置了post
路线,则需要将其更改为put
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.