了解 Rails 实例变量 [英] Understanding Rails Instance Variables

查看:63
本文介绍了了解 Rails 实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个使用实例变量的 products_controller.我对 Ruby 中的实例变量的理解是,您可以在同一个类的不同方法中使用它们.那么为什么我们在 Rails 应用程序的多个方法中使用相同的实例变量呢?下面我们有一个实例变量@product设置了两次,new action中的@product变量在create action中使用时是不是被覆盖了?

In my application I have a products_controller that makes use of instance variables. My understanding of instance variables in Ruby is that you can use them within different methods of the same class. So why is it that we use the same instance variable in multiple methods of rails apps? Below we have an instance variable @product set twice, is the @product variable in the new action not overwritten when we use it in the create action?

我对这些变量在同一个类的方法中的范围有点困惑.

I am just a little confused as to the scope of these variables within methods of the same class.

  def new
    @product = Product.new
  end

  def create
    @product = Product.new(product_params)

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render :show, status: :created, location: @product }
      else
        format.html { render :new }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

推荐答案

实例变量在实例类范围内.在 Ruby on Rails 中,由于 API 的构建方式,实例变量也可在视图中使用.

Instance variables are in the instance class scope. In Ruby on Rails, because the way how that API was built, instance variables are also available in the views.

您需要注意 new 和 create 方法通常用于不同的 ProductsController 实例.

You need to be aware of that new and create methods are commonly used in different ProductsController instances.

第一个请求:GET http://localhost:3000/product/new

当您请求 new 操作(我想这是一个表单)时,Rails API 实现在给定点创建一个 ProductsController 实例并发送 new 消息到该实例(调用新方法).然后,实例变量@product 被创建并在任何方法中可用,或者在操作呈现的任何视图中.在给定的时间点,Rails 回复一个网页,类实例及其所有实例变量都被销毁(不再可用).

When you ask for the new action (I suppose that is a form), Rails API implementation at a given point creates an instance of ProductsController and sends the new message to that instance (calls the new method). Then, the instance variable @product is created and available in any method, or in any view that the action renders. At a given point, Rails replies with a web page and the class instance, and all its instance variables, are destroyed (won't be available anymore).

第二个请求:POST http://localhost:3000/product/create强>

当您提交数据库持久化表单时,会再次创建一个新的控制器实例,并调用 create 方法.因为是新实例,@product 没有任何价值.

When you submit the form for database persistence, again a new controller instance is created, and the create method is called. Because is a new instance, the @product doesn't have any value.

但是请注意,渲染视图(就像它在 new 操作中发生的那样)和重定向(就像你在 create 操作中所做的那样)之间是有区别的如果 @product.save 为真).当你渲染时,你保持在同一个控制器实例中,随着你重定向,新的服务器请求发生,所以之前的控制器实例被销毁并创建一个新的控制器实例.

Note, however, that there is a difference between rendering a view (like its happening in the new action) and a redirect (like you do in the create action if @product.save is true). When you render, you remain in the same controller instance, with you redirect, new server request happens, so the previous controller instance is destroyed and a new controller instance is created.

之前的行动

before_action 在您实际开始执行操作代码之前被调用.从 Rails 的角度来看,动作不是 Ruby 方法.类方法是该操作的定义:

before_action is called before you actually start executing the action code. In Rails perspective, an action is not a Ruby method. The class method is the definition of that action:

来自 Rails 指南:

From Rails guides:

控制器是一个继承自 ApplicationController 的 Ruby 类并且拥有与任何其他类一样的方法.当您的申请收到请求,路由将决定哪个控制器和action 要运行,然后 Rails 创建该控制器的一个实例并运行与操作同名的方法.

A controller is a Ruby class which inherits from ApplicationController and has methods just like any other class. When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the method with the same name as the action.

动作充当由路由确定的入口点.如果您在 new 中调用 create,则不会再次触发该 before_action.

The action acts as an entry point determined by the routes. If you call create inside new, it won't trigger that before_action again.

这篇关于了解 Rails 实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆