加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails 4强参数

发布时间:2020-12-17 01:53:53 所属栏目:百科 来源:网络整理
导读:Rails 4处理强参数似乎很有趣: def UsersController ActionController::Base def update @user = current_user if @user.update_attributes(user_profile_parameters) redirect_to home_path,notice: "Your profile has been successfully updated." else r
Rails 4处理强参数似乎很有趣:

def UsersController < ActionController::Base
  def update
    @user = current_user

    if @user.update_attributes(user_profile_parameters)
      redirect_to home_path,notice:  "Your profile has been successfully updated."
    else
      render action: "edit"
    end
  end

  private
  def user_profile_parameters
    params.require(:user).permit(:name,:password,:password_confirmation,:email)
  end
end

我想知道,在Rails 3中是不是可能?是的,或许只有1行,而不是只有1行.但是,那里没有什么新东西,它只是一个手动创建的允许参数列表,事实上,它只是一个哈希,不是吗?或者它有更深层次的目的吗?

解决方法

我想它在将rails 4作为一个单独的gem发布之前就存在了(之前的方式)
https://github.com/rails/strong_parameters

rails 4默认使用它,如果你想将它与rails 3一起使用,只需在你的gemfile中获取gem即可开始:).

在gem的github链接上,他们也写了关于如何使用它的精彩文档,所以我想你应该没有任何问题使用它.

当然,没有什么新东西,
您可以通过编写自己的过滤器来手动执行此过滤,并且如果哈希中的深层嵌套哈希和数组是动态创建的(未修复),则可能会有些棘手

“”是的,而不是只有1行,它将需要3行“”,这就是大多数宝石所做的,我们使用它们而不是重新发明轮子,只关注我们的业务逻辑.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读