Strong parameters Rails强参数示例中的“current_account.people.find”是什么?
我是Rails新手,目前正在学习Rails 4中的强参数,并遵循官方文档中的以下示例:Strong parameters Rails强参数示例中的“current_account.people.find”是什么?,strong-parameters,Strong Parameters,我是Rails新手,目前正在学习Rails 4中的强参数,并遵循官方文档中的以下示例: `class PeopleController < ActionController::Base # Using "Person.create(params[:person])" would raise an # ActiveModel::ForbiddenAttributes exception because it'd # be using mass assignment wit
`class PeopleController < ActionController::Base
# Using "Person.create(params[:person])" would raise an
# ActiveModel::ForbiddenAttributes exception because it'd
# be using mass assignment without an explicit permit step.
# This is the recommended form:
def create
Person.create(person_params)
end
# This will pass with flying colors as long as there's a person key in the
# parameters, otherwise it'll raise an ActionController::MissingParameter
# exception, which will get caught by ActionController::Base and turned
# into a 400 Bad Request reply.
def update
redirect_to current_account.people.find(params[:id]).tap { |person|
person.update!(person_params)
}
end
private
# Using a private method to encapsulate the permissible parameters is
# just a good pattern since you'll be able to reuse the same permit
# list between create and update. Also, you can specialize this method
# with per-user checking of permissible attributes.
def person_params
params.require(:person).permit(:name, :age)
end
end`
问题1:
current_account.people.find在更新方法中的含义是什么
问题2:
有人能解释一下这个人的方法吗。person_params方法中的params是什么
current_account很可能是返回account实例的私有方法。current_account.people.findparams[:id]在people表中搜索属于当前_帐户且id为params[:id]的人员。Objecttap是一种ruby方法,它使用当前对象生成一个块,然后返回该对象。在这种情况下,Person实例在块内更新,并从tap返回。最后,redirect_to是一种控制器方法,它将请求重定向到不同的路径。重定向到可以接受许多不同类型的参数,包括ActiveRecord模型、字符串或符号。将ActiveRecord模型传递给它会将请求重定向到模型的资源路径,该路径在routes.rb中定义。在本例中,该路径很可能是/people/:id
params对象是包含参数名称和值的哈希。例如,request/people?name=Joe&age=34将生成以下params对象:{name:'Joe',age:'34'}
谢谢你的快速回复。至于问题1,正如你提到的,活期账户最有可能是一种私人方法,那么,它最有可能在什么地方被定义?你能给我举个例子吗?另外,从问题2开始,params对象在哪里定义?PeopleController类是否从某个地方继承了它?我想我需要一个完整的示例,可能是一个简单而简短的虚拟代码库,以便以具体的方式理解这一点。非常感谢您的帮助。