Tag: 强大的参数

Rails – 空数组的强参数

我发送一个关联ID的数组,说foo_ids到我的控制器。 为了允许一组值,我使用: params.permit(foo_ids: []) 现在,问题是,如果我发送一个空数组foo_ids ,该参数被忽略。 不要将所有的foos都foos为空数组,因为foo_ids是不允许的,所以这个关联是独立的。 这可能是因为一个空数组在轨中被转换为nil ,并且nil值被忽略,因为强参数正在寻找一个标量值数组,而不是单个标量值。 任何人都可以提出一个很好的解决方法吗? 谢谢! 附加信息 在更新控制器操作中,我需要能够处理两种情况。 我需要能够将foo_ids设置为一个空数组。 我也需要能够忽略foo_ids如果我只是想更新另一个领域。 如果nil不适用于第二种情况, foo_ids设置为空数组。

Rails 4.0 Strong参数使用指向散列的键来嵌套属性

我正在玩Rails 4.xtesting版,并尝试使用carrierwave来获取嵌套属性。 不知道我在做什么是正确的方向。 经过四周search,然后最终看到了轨道来源和强大的参数,我发现下面的笔记。 # Note that if you use +permit+ in a key that points to a hash, # it won't allow all the hash. You also need to specify which # attributes inside the hash should be whitelisted. https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/strong_parameters.rb 所以它说你必须指定每一个单一的属性,我试着以下几点: 帕拉姆的例子: {"utf8"=>"✓", "authenticity_token"=>"Tm54+v9DYdBtWJ7qPERWzdEBkWnDQfuAQrfT9UE8VD=", "screenshot"=>{ "title"=>"afs", "assets_attributes"=>{ "0"=>{ "filename"=>#<ActionDispatch::Http::UploadedFile:0x00000004edbe40 @tempfile=#<File:/tmp/RackMultipart20130123-18328-navggd>, @original_filename="EK000005.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; […]

devise和强大的参数

我想知道如何整合这两个gem(devise+强大的参数),因为强有力的参数可能会被添加到轨道核心在4.0 欢迎任何帮助,谢谢

如何为编辑操作指定devise_parameter_sanitizer?

我已经添加了devise到我的Rails 4应用程序,并成功地添加了用户名等我的用户模型。 此外,我可以使用lazy way™存储这些字段,即 class ApplicationController < ActionController::Base before_filter :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :middlename, :lastname) } end end 不过,我试过了 def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :middlename, :lastname) } devise_parameter_sanitizer.for(:edit) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :middlename, :lastname) } end 但是这并没有像预期的那样工作(当编辑操作调用时,用户名不被存储)。 还有什么我需要做的,为了得到这个工作? 谢谢!

Rails 4未授权的数组参数

我在我的模型中有一个数组字段,我试图更新它。 我强有力的参数方法如下 def post_params params["post"]["categories"] = params["post"]["categories"].split(",") params.require(:post).permit(:name, :email, :categories) end 我在我的控制器中的行为如下 def update post = Post.find(params[:id] if post and post.update_attributes(post_params) redirect_to root_url else redirect_to posts_url end end 然而,每当我提交更新的post,在我的发展日志中我看到 Unpermitted parameters: categories 通过的参数是 Parameters: {"utf8"=>"✓", "authenticity_token"=>"auth token", "id"=>"10", "post"=>{"name"=>"Toni Mitchell", "email"=>"eileen_hansen@hayetokes.info", "categories"=>",2"}} 我想这与事实有关,因为一切看起来都很好,属性categories是一个数组。 再一次,我可能是错的。 那么,我的代码出了什么问题?为什么在明确允许的情况下不让我保存类别字段呢? 谢谢。

Rails 4强大的参数:允许所有的属性?

我正在构build一个带有Rails 4强大参数的Web应用程序。 在构buildpipe理员后台控制器时,我想知道允许所有模型属性的最佳方法是什么? 现在,我写了这个: def user_params params.require(:user).permit(User.fields.keys) end 你觉得更好的方法吗?

Rails和Devise强大的参数

我正在使用devise的轨道4.0分支与ruby2.0.0p0和Rails 4.0.0.beta1。 这是一个问题,我正在检查我是否按照正确的方式进行,或者如果有其他事情我应该做。 我相信很多转向Rails 4.0的人都面临同样的问题(在Google上search类似的东西之后)。 我已阅读以下链接: devise和强大的参数 https://gist.github.com/kazpsp/3350730 https://github.com/plataformatec/devise/tree/rails4#strong-parameters 现在使用devise我创build了一个用户模型,我使用上面的要点创build了以下控制器(并确保将其包含在我的路线文件中)。 我的额外参数是first_name和last_name。 class Users::RegistrationsController < Devise::RegistrationsController def sign_up_params params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation) end def account_update_params params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password) end private :sign_up_params private :account_update_params end 还有什么我应该做的? 这是从现在开始做事情的最佳方式(自从删除attr_accessor之后)。 我的表格似乎工作正常(新的和更新)。 要点说使用“resource_params”,但总是在我的服务器日志中给“未经许可的参数”错误。

Rails 4:将属性插入参数

在Rails 3中,可以像这样在params中插入一个属性: params[:post][:user_id] = current_user.id 我试图在Rails 4中做类似的事情,但没有运气: post_params[:user_id] = current_user.id . . . . private def post_params params.require(:post).permit(:user_id) end Rails忽略了这个插入。 它不会抛出任何错误,只是悄然失败。