简体   繁体   English

Ruby on Rails中的User.new解释? (摘自Michael Hartl的教程)

[英]Explanation of User.new in Ruby on Rails? (From Michael Hartl's tutorial)

I have searched everywhere to try to find an explanation of how this works/what its purpose is, but I cant find anything helpful. 我到处搜索以试图找到对此功能的解释/目的是什么,但是我找不到任何有用的解释。 I am doing Michael Hartl's tutorial, and my question is mainly about the two actions: 'new' and 'create'. 我正在做Michael Hartl的教程,我的问题主要是关于两个动作:“ new”和“ create”。 The new action has the following: 新操作具有以下内容:

def new
  @user = User.new
end

In the view corresponding to the 'new' action, there is a form_for helper, where users can type in their attributes and hit submit. 在与“新”操作相对应的视图中,有一个form_for辅助程序,用户可以在其中键入其属性并点击Submit。 As expected, the beginning of the form_for helper looks like this: form_for(@user) 不出所料,form_for帮助程序的开头看起来像这样:form_for(@user)

However here is where I am stumped... In the create action, there is the following code: 但是,这是我感到难过的地方...在create动作中,有以下代码:

def create
  @user = User.new(user_params)  
  #user_params is a function we defined which simply returns the permitted params from the user.

What is the purpose of @user = User.new in the 'new' action? @user = User.new在“ new”操作中的作用是什么? What does User.new even accomplish? User.new甚至可以完成什么工作? I am assuming that the instance variable @user is necessary to pass to the form, but in that case, why do we have to redeclare an @user isntance variable in 'create'? 我假设必须将实例变量@user传递给表单,但是在那种情况下,为什么我们必须在'create'中重新声明@user istance变量? Isn't it sufficient to have only @user = User.new(user_params) in our 'create' action? 在我们的“创建”操作中仅使用@user = User.new(user_params)是否足够? Is the User.new somehow necessary to make the form function properly? 为了使表单正常运行,是否需要User.new?

I am mainly just trying to figure out what @user = User.new accomplishes in our 'new' action and its corresponding 'new' view (with the form), and why it is necessary when we have a 'create' action which actually CREATES the object. 我主要是想弄清楚@user = User.new在我们的“ new”操作及其对应的“ new”视图(带有表单)中完成了什么,以及为什么当我们有一个“ create”操作实际上是必要的时,为什么有必要创建对象。 ANY help is SO GREATLY APPRECIATED. 非常感谢您的帮助。 Thank you all for always doing your best to explain. 谢谢大家一直尽力解释。 Thank you ahead of time to anyone who answers this. 提前感谢所有回答此问题的人。

The new and create are different actions. new和create是不同的操作。 New is called when you get the new route. 当您获得新路线时,将调用新路线。 Create is called when you post to the new route. 发布到新路线时将调用Create。 So, you have to create the user in new so they're available in the form. 因此,您必须以新方式创建用户,以便可以在表单中使用它们。 You have to create the user with the form contents in create so you can save it to the database. 您必须使用create中的表单内容来创建用户,以便将其保存到数据库中。

You can't assume that the request to new will go to the same rails instance as the request to create. 您不能假定对new的请求将与要创建的请求进入相同的rails实例。 It's common to run multiple instances of your app behind a proxy. 通常在代理后运行您的应用程序的多个实例。

It's called object orientated programming 这就是所谓的object orientated programming

在此处输入图片说明


HTTP HTTP

In Ruby, each variable you define is an object . 在Ruby中,您定义的每个变量都是一个object These objects are then manipulated throughout each instance of the app. 然后,在应用程序的每个实例中操纵这些对象。

In traditional ( stateful ) applications, your computer is able to store a number of objects in memory, and since your application is always in state , you'll be able to manipulate them from a single invocation. 在传统的( 有状态 )应用程序中,您的计算机能够在内存中存储许多对象 ,并且由于您的应用程序始终处于状态 ,因此您可以通过一次调用来操纵它们。

In HTTP ( stateless ) applications, you have to rebuild the objects with each call. 在HTTP( 无状态 )应用程序中,您必须在每次调用时重建对象。 Because your application doesn't retain state (memory) between each request, you have to build the objects again. 因为您的应用程序不会在每个请求之间保留状态(内存),所以您必须再次构建对象。

This is why Rails "variables" are called with a class function on the model (class): User.find ... 这就是为什么在模型(类)上使用类函数调用Rails“变量”的原因: User.find ...

-- -

Thus, when using the following: 因此,使用以下命令时:

#app/controllers/your_controller.rb
class YourController < ApplicationController
   def new
      @user = User.new #-> invokes a new user object
   end

   def create
      @user = User.new user_params #-> invokes a new user object & populates with your params
      @user.save #-> "saves" the new record
   end

   def show
      @user = User.find params[:id] #-> stateless means you have to rebuild the object again
   end
end

... what you're doing is rebuilding the object each time your actions are invoked. ...您正在做的是在每次调用动作时重建对象。

This is one of the pitfalls of using HTTP - your server is "dumb" and cannot retain state between requests. 这是使用HTTP的陷阱之一-您的服务器“笨拙”,无法在请求之间保留状态。 Although Rails does a great job at making it a seamless process, it can be difficult if you haven't got your head around it yet. 尽管Rails在使它成为无缝过程方面做得很出色,但是如果您还不了解的话可能会很困难。

Most generally, users enter data and we programmer-types traditionally store it in a relational database. 通常,用户输入数据,而我们的程序员通常将数据存储在关系数据库中。

This creates an "impedance" between a relational model (ie, tables and rows) and an object-oriented one (roughly, classes and instances). 这在关系模型(即表和行)和面向对象的模型(大致是类和实例)之间创建了“阻抗”。

ORMs like ActiveRecord help abstract much of this tedium, and in this way model instances--such as those we're creating in controller actions--serve as helpful containers for data. 像ActiveRecord这样的ORM可以帮助抽象大量的乏味,并且通过这种方式,模型实例(例如我们在控制器操作中创建的实例)可以用作数据的有用容器。

This lets us easily represent models in views when gathering user input, and bind inputs to model attributes when persisting it (basic CRUD). 这使我们可以在收集用户输入时轻松地在视图中表示模型,并在持久存储时将输入绑定到模型属性(基本CRUD)。

The separate controller actions merely represent these two steps in the process, as any Web-based app ultimately speaks HTTP. 单独的控制器动作仅代表该过程中的两个步骤,因为任何基于Web的应用程序最终都使用HTTP。

This is really the whole benefit and genesis of Rails and similar MVC frameworks, born in a time of relational databases and server-side rendering. 这实际上是在关系数据库和服务器端渲染时代诞生的Rails和类似的MVC框架的全部优点和起源。 (Though they are increasingly coping with and adapting to an environment that now includes document/object-oriented databases and client-scripted front-ends.) (尽管他们越来越多地应对和适应现在包括文档/对象导向数据库和客户端脚本前端的环境。)

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

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