简体   繁体   English

为什么我不需要将参数传递给 article_params?

[英]How come I don't need to pass params to article_params?

How does the article_params method get access to params without it being passed as a parameter? article_params方法如何在不将其作为参数传递的情况下访问params Thanks in advance.提前致谢。

class ArticlesController < ApplicationController
  private def article_params
    params.require(:article).permit(:title, :text)
  end
  def create
    @article = Article.new(article_params)

    if @article.save
      redirect_to @article
    else
      render 'new'
    end
  end

Because params is not a variable, it's a method in ActionController::Base which returns the parameters passed in by the call, and, being a method, is available to be called by any method in any controller.因为params不是变量,它是ActionController::Base的一个方法,它返回调用传入的参数,并且作为一个方法,可以被任何控制器中的任何方法调用。 In fact you can reference params in views, so it's not only a method, it's a helper method.实际上你可以在视图中引用params ,所以它不仅是一个方法,它还是一个辅助方法。

As described in https://api.rubyonrails.org/classes/ActionController/Base.htmlhttps://api.rubyonrails.org/classes/ActionController/Base.html 中所述

All request parameters, whether they come from a query string in the URL or form data submitted through a POST request are available through the params method which returns a hash.所有请求参数,无论它们来自 URL 中的查询字符串还是通过 POST 请求提交的表单数据,都可以通过返回哈希值的 params 方法获得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM