如何防止非法参数

问题描述 投票:0回答:1

在 Rails 7.1 应用程序上,我有下面的表格

<%= form_with model: @post do |form| %>
  <%= form.text_area :body %>
  <%= form.submit %>
<% end %>

以及相关的动作。

def update
  @post = Post.find(params[:id])

  if @post.update(post_params)
    ...
  end
end

private

def post_params
  params.require(:post).permit(:body)
end

当我提交表单时,以下参数将发送到控制器。

Parameters: {"authenticity_token"=>"[FILTERED]", "post"=>{"body"=>""}, "commit"=>"Update Post", "id"=>"1"}

有办法防止

Unpermitted parameter
被提升吗?

不允许的参数::_method、:authenticity_token、:button、:id。上下文:{ 控制器:PostsController,操作:更新,请求:#ActionDispatch::Request:0x0000000110733988,参数:{“_method”=>“补丁”,“authenticity_token”=>“[FILTERED]”,“post”=>{ "body"=>""}, "commit"=>"更新帖子", "controller"=>"帖子", "action"=>"更新", "id"=>"1"} }

我在这里遗漏了什么吗?

ruby-on-rails strong-parameters
1个回答
0
投票

config用于不允许的参数

# To take no action:
config.action_controller.action_on_unpermitted_parameters = false
# To emit an `ActiveSupport::Notifications.instrument` event
# on the `unpermitted_parameters.action_controller` topic
# and log at the DEBUG level:
config.action_controller.action_on_unpermitted_parameters = :log
# To raise a ActionController::UnpermittedParameters exception:
config.action_controller.action_on_unpermitted_parameters = :raise

您可以选择需要的

© www.soinside.com 2019 - 2024. All rights reserved.