Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未定义的局部变量或方法“user';_Ruby On Rails_Ruby_Devise - Fatal编程技术网

Ruby on rails 未定义的局部变量或方法“user';

Ruby on rails 未定义的局部变量或方法“user';,ruby-on-rails,ruby,devise,Ruby On Rails,Ruby,Devise,我得到了这个错误:未定义的局部变量或#的“user”方法指向配置文件控制器第9行,“def更新方法后的第一行”,顺便说一句,我正在为我的用户使用designe class ProfileController < ApplicationController before_action :authenticate_user! def index @user = current_user end def update current_user.update(user.params)

我得到了这个错误:未定义的局部变量或#的“user”方法指向配置文件控制器第9行,“def更新方法后的第一行”,顺便说一句,我正在为我的用户使用designe

class ProfileController < ApplicationController
before_action :authenticate_user!

def index
  @user = current_user
end

def update
  current_user.update(user.params) 
  redirect_to root_path
end


private
  def user_params
  params.require(user).permit(:first_name, :last_name)
  end
end
以下是生成的模式设计:

ActiveRecord::Schema.define(version: 20160406005549) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "users", force: :cascade do |t|
 t.string   "email",                  default: "", null: false
 t.string   "encrypted_password",     default: "", null: false
 t.string   "reset_password_token"
 t.datetime "reset_password_sent_at"
 t.datetime "remember_created_at"
 t.integer  "sign_in_count",          default: 0,  null: false
 t.datetime "current_sign_in_at"
 t.datetime "last_sign_in_at"
 t.inet     "current_sign_in_ip"
 t.inet     "last_sign_in_ip"
 t.datetime "created_at",                          null: false
 t.datetime "updated_at",                          null: false
 t.string   "first_name"
 t.string   "last_name"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name:    "index_users_on_reset_password_token", unique: true, using: :btree

end

您的代码有一些地方出错。首先,您在控制器中调用一个未定义的变量
user
应为符号,user.params应为
user\u params

class ProfileController < ApplicationController
before_action :authenticate_user!

def index
  @user = current_user
end

def update
  current_user.update(user_params) 
  redirect_to root_path
end


private
  def user_params
  params.require(:user).permit(:first_name, :last_name)
  end
end
class ProfileController
您还需要更新路由以支持您描述的用户操作:

路由内。rb:

资源:用户

最后,要使用Desive helper方法,请确保在routes.rb的顶部也有Desive routes:


design\u:users

user.params
应该是
user\u-params
我花了大约10个小时的时间放松自己,直到凌晨3点左右才醒来。这总是最愚蠢的错误。谢谢
ActiveRecord::Schema.define(version: 20160406005549) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "users", force: :cascade do |t|
 t.string   "email",                  default: "", null: false
 t.string   "encrypted_password",     default: "", null: false
 t.string   "reset_password_token"
 t.datetime "reset_password_sent_at"
 t.datetime "remember_created_at"
 t.integer  "sign_in_count",          default: 0,  null: false
 t.datetime "current_sign_in_at"
 t.datetime "last_sign_in_at"
 t.inet     "current_sign_in_ip"
 t.inet     "last_sign_in_ip"
 t.datetime "created_at",                          null: false
 t.datetime "updated_at",                          null: false
 t.string   "first_name"
 t.string   "last_name"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name:    "index_users_on_reset_password_token", unique: true, using: :btree

end
class ProfileController < ApplicationController
before_action :authenticate_user!

def index
  @user = current_user
end

def update
  current_user.update(user_params) 
  redirect_to root_path
end


private
  def user_params
  params.require(:user).permit(:first_name, :last_name)
  end
end