简体   繁体   English

如何获得带有参数的正确信息?

[英]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.

相关问题 如何从选择标记中获取ID作为参数? - How can I get the id from select tag as params? 如何重新排列我的参数以获得所需的结构? - How can I rearrange my params to get the desired structure? 如何通过link_to发送2个参数,如果页面上存在1个参数,我可以通过JS获取这个值 - How send 2 params in action by link_to, if 1 params exist on page and I can get this value by JS 我该如何解决这个问题:缺少参数? - How can i solve this: params is missing? 如何循环使用Rails参数? - How can I loop the rails params? RSpec controller 测试无法获取 id 参数。 我不知道如何解决这个问题 - RSpec controller test can't get id params. I don't know how to fix this ROR:如何在不重新加载浏览器的情况下从控制器获取参数? - ROR:How can I get the params from controller without reloading the browser? 保存前如何从post_params获取和使用对象的ID - How can I get and use the id of an object from post_params before saving 使用嵌套属性在RSpec中测试POST #create-无法正确获取参数哈希 - Testing POST #create in RSpec with nested attributes - can't get params hash correct 我如何使用form_for以便在controller#create中使用参数获取hidden_​​field值 - How can i use form_for so as to get a hidden_field value using params in controller#create
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM