简体   繁体   English

我不明白的几个Rails问题

[英]Couple of Rails issues that i didn't understand

I'm just learning rails with this tutorial and there are couple of things i can't understand: 我只是通过教程学习Rails,有些事情我不明白:

1- whats the difference between these two assignments? 1-这两个作业之间有什么区别? why can i use it in the same helper? 为什么我可以在同一个助手中使用它?

  def current_employee=(employee)
    @current_employee = employee
  end

  def current_employee
    remember_token = Employee.hash(cookies[:remember_token])
    @current_employee ||= Employee.find_by(remember_token: remember_token)
  end

2- what's the difference between these 2 update functions, and why the json needed here? 2-这两个更新功能之间有什么区别,为什么这里需要json?

  def update
    respond_to do |format|
      if @employee.update(employee_params)
        format.html { redirect_to @employee, success: 'Employee was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @employee.errors, failure: :unprocessable_entity }
      end
    end
  end

and

def update
    @employee = Employee.find(params[:id])
    if @employee.update_attributes(employee_params)
      # Handle a successful update.
    else
      render 'edit'
    end
end

1- whats the difference between these two assignments? 1-这两个作业之间有什么区别? why can i use it in the same helper? 为什么我可以在同一个助手中使用它?

  def current_employee=(employee)
    @current_employee = employee
  end

  def current_employee
    remember_token = Employee.hash(cookies[:remember_token])
    @current_employee ||= Employee.find_by(remember_token: remember_token)
  end

The first one, current_employee=(employee) , is called a setter , because you set a new value on @current_employee . 第一个称为current_employee=(employee) ,称为setter ,因为您在@current_employee上设置了新值。 The second one, current_employee , is called getter , because you get the current value of the @current_employee . 第二个称为current_employee ,称为getter ,因为您可以获取@current_employee的当前值。 More info about setters and getters . 有关setter和getter的更多信息

In the getter method, the ||= is used for memoization . 在getter方法中, ||=用于memoization So the first time you call current_employee the value of Employee.find_by(remember_token: remember_token) is assigned to @current_employee . 因此,第一次调用current_employeeEmployee.find_by(remember_token: remember_token) @current_employee Employee.find_by(remember_token: remember_token)值将分配给@current_employee In subsequent calls to the getter method, you retrieve the same value. 在随后的对getter方法的调用中,将检索相同的值。

2- what's the difference between these 2 update functions, and why the json needed here? 2-这两个更新功能之间有什么区别,为什么这里需要json?

  def update
    respond_to do |format|
      if @employee.update(employee_params)
        format.html { redirect_to @employee, success: 'Employee was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @employee.errors, failure: :unprocessable_entity }
      end
    end
  end

and

def update
    @employee = Employee.find(params[:id])
    if @employee.update_attributes(employee_params)
      # Handle a successful update.
    else
      render 'edit'
    end
end

Both update methods are esentially the same. 两种update方法本质上是相同的。 The last one is responding only to HTML format. 最后一个仅响应HTML格式。 The first example is responding to both, HTML and JSON formats. 第一个示例同时响应HTML和JSON格式。 If you don't need to response to JSON format, you can stay with the last one. 如果您不需要响应JSON格式,则可以保留最后一个格式。

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

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