Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.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 RailsCast 274中的Rails 4 strong属性_Ruby On Rails_Ruby_Ruby On Rails 4_Railscasts - Fatal编程技术网

Ruby on rails RailsCast 274中的Rails 4 strong属性

Ruby on rails RailsCast 274中的Rails 4 strong属性,ruby-on-rails,ruby,ruby-on-rails-4,railscasts,Ruby On Rails,Ruby,Ruby On Rails 4,Railscasts,我不熟悉Rails。特别是在处理Rails 3和Rails 4之间的异常情况时。我一直在学习RailsCast和MHartl的教程 我使用下面链接的问题中的答案成功地获得了RailsCast 274中的代码: 我担心的是,此修复程序将使我在将来容易受到问题的影响,无论是安全性还是其他方面。如果有一个正确的方法来做到这一点,我想知道。这是我的代码块: class PasswordResetsController < ApplicationController def create

我不熟悉Rails。特别是在处理Rails 3和Rails 4之间的异常情况时。我一直在学习RailsCast和MHartl的教程

我使用下面链接的问题中的答案成功地获得了RailsCast 274中的代码:

我担心的是,此修复程序将使我在将来容易受到问题的影响,无论是安全性还是其他方面。如果有一个正确的方法来做到这一点,我想知道。这是我的代码块:

class PasswordResetsController < ApplicationController
  def create
    user = User.find_by_email(params[:email])
    user.send_password_reset if user
    redirect_to root_url, :notice => "Email sent with password reset instructions."
  end

  def edit
    @user = User.find_by_password_reset_token!(params[:id])
  end

  def update
    @user = User.find_by_password_reset_token!(params[:id])
    if @user.password_reset_sent_at < 2.hours.ago
      redirect_to new_password_reset_path, :alert => "Password reset has expired."
    elsif @user.update_attributes(params.require(:user).permit(:password, :password_confirmation))
      redirect_to root_url, :notice => "Password has been reset."
    else
      render :edit
    end
  end
end

你需要先设置你的参数。在类中定义私有方法

private
def model_params
  params.require(:model).permit(:list :all :your :attributes)
end
然后,当您进行更新时,请使用以下方法:

@model.update(model_params)
批量分配在rails中是一件很酷的事情,但您需要确保自己受到保护


希望这有帮助

太棒了,这正是我想要的。