Tag: 实例变量

Rails模型中的实例变量

我想在我的Rails模型中初始化一个实例变量,它将包含一个数组,我想在我的模型中的其他方法中访问这个变量。 我试过这个: class Participant < ActiveRecord::Base @possible_statuses = [ 'exists', 'paired', 'quiz_finished', 'quiz_results_seen', 'money_sent' ] def statuses @possible_statuses end 但是当我使用rails console尝试以下操作时: Participant.first.statuses 我回来了nil 🙁 为什么会这样? 有没有办法完成我想要完成的任务?

response_with中模型变量的用途

在Rails 3中,使用新的respond_to和respond_with构造,我这样做: class SurveysController < ApplicationController respond_to :html def survey @program_id = params[:program_id] @participant_id = params[:participant_id] respond_with [@program_id, @participant_id] end end 显示视图(survey.html.erb)时,变量program_id,@ participant_id都已正确设置。 但是,如果我从respond_with中省略它们,如下所示: class SurveysController < ApplicationController respond_to :html def survey @program_id = params[:program_id] @participant_id = params[:participant_id] @foo = "foo" respond_with @foo end end 在视图中仍然可以看到相同的两个实例变量。 换句话说,来自操作的所有实例变量都可以在视图中使用。 问题:为什么我要将实例变量放在respond_to行上?

before_filter方法中的隐藏实例变量

在私有方法中隐藏实例变量初始化是一个好习惯吗? 例如,我有一个用户控制器,其中包含一些操作: class UsersController < ApplicationController before_filter :get_user, only: [:show, :edit, :update, :destroy] before_filter :set_user, only: [:new, :create] def index @users = User.all end def show end def new end def edit end def create if @user.save redirect_to @user, notice: 'User was successfully created.' else render action: 'new' end end def update if @user.update_attributes(params[:user]) redirect_to […]

在Rails 3中初始化变量的位置

我在过去3个月里一直在发现Ruby on Rails,我很着迷。 但有些事我不知道哪种方法最好。 现在我正在堆栈一些变量的初始化。 所有应用程序都在ajax上运行,因此所有实例变量只应初始化一次。 我已经尝试在applicationController中编写一个方法’initialize’,它只运行一次,但问题是我正在访问尚未初始化的请求对象。 我在applicationController中尝试了一个before_filter,但是每次请求都会运行它,即使它是一个ajax请求。 有谁知道如何使用请求对象中的值初始化实例变量? 是的,可能是一个新手问题。 我正在使用Ruby 1.8.7和Rails 3.提前感谢

类中的实例变量未保存

我通过before_validation过滤我的PersonalInfo类的所有实例值,如下所示: before_validation :strip_tabs def strip_tabs self.instance_variables.map do |attr| value = self.instance_variable_get(attr) if value.present? && value.kind_of?(String) encoding_options = { :invalid => :replace, # Replace invalid byte sequences :undef => :replace, # Replace anything not defined in ASCII :replace => ”, # Use a blank for those replacements :universal_newline => true # Always break lines with \n […]

如何在Ruby on Rails中使用带有邮件程序的实例变量?

我在邮件程序的模型中创建了一个实例变量(@user),并希望在视图中访问它? 但它给了我一个错误(@user = nil)。 将变量传递给视图(电子邮件正文)的最佳方法是什么? 谢谢,奇拉格

实例变量未使用重定向设置

什么会导致我的实例变量@product没有为重定向设置/传递。 Product是ActiveModel对象,而不是ActiveRecord。 更具体地说,@ product变量没有出现在redirect_to(new_services_path)或redirect_to(home_path)页面中。 由于@product变量需要在我的页脚中填充每页上的表单。 Application_controller: class ApplicationController < ActionController::Base before_filter :set_product private def set_product @product ||= Product.new end end Product_controller: def new end def create @product = Product.new(params[:product]) if @product.category == “wheels” redirect_to(new_services_path) else redirect_to(home_path) end end 与此原始post相关的问题。 通过多个部分传递变量(轨道4)

为什么使用实例变量将控制器与视图“连接”?

这是一个概念性问题,我无法在SO中找到答案,所以我在这里: 为什么实例变量用于连接控制器和视图? 我们不是有两个不同类的两个不同对象(Controller vs Views)。 因此,当渲染视图时,我们处于不同的上下文中,但我们正在使用另一个对象的实例变量? 这不是以某种方式破坏封装吗? Rails如何设法从一个对象到另一个对象进行匹配? 它是否将控制器的所有实例变量克隆到视图中?

在ruby中直接使用实例变量是不好的forms?

你应该总是在ruby中创建访问者(用于阅读和/或写作)吗? 如果你有一个不打算在外面重用的类,我不能直接使用实例变量吗? 我遇到的一个问题是在测试中存根@instance_vars是有问题的。

Ruby可以子类化实例变量_overwrite_超类(同名)吗?

在“ruby编程语言”一书的第7.3.5节“inheritance和实例变量”中说: 因为实例变量与inheritance无关,所以子类使用的实例变量不能“遮蔽”超类中的实例变量。 如果子类使用与其祖先之一使用的变量同名的实例变量,它将覆盖其> ancestor变量的值。 这可以有意地完成,以改变祖先的行为,或者可以无意中完成。 在后一种情况下,几乎可以肯定会导致错误。 与前面描述的私有方法的inheritance一样,这是另一个原因,当您熟悉(并控制)超类的实现时,扩展Ruby类是安全的。 我做了自己的测试,但似乎来自子类的实例变量不会影响超类 我的Ruby版本 bob@bob-ruby:~$ ruby –version ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux] bob@bob-ruby:~$ 以下是代码 class Point attr_accessor :x,:y def initialize(x,y) @x,@y=x,y end end class Point3D p=Point.new(1,2) => # irb(main):053:0> q=Point3D.new(4,5,6) => # irb(main):054:0> qx => 4 irb(main):055:0> px => 1 irb(main):056:0>