Tag: 状态

状态机,模型validation和RSpec

这是我当前的类定义和规范: class Event :not_started do event :game_started do transition :not_started => :in_progress end event :game_ended do transition :in_progress => :final end event :game_postponed do transition [:not_started, :in_progress] => :postponed end state :not_started, :in_progress, :postponed do validate :end_time_before_final end end def end_time_before_final return if end_time.blank? errors.add :end_time, “must be nil until event is final” if end_time.present? […]

在提交和重新加载页面后,我的表单复选框如何保持其状态?

我有一个由复选框组成的表单,它在索引页面上过滤记录,如何在表单提交并重新加载页面后,如何使表单中的复选框保持其已检查状态? 我已经使用jQuery-UI使复选框看起来更好。 我是任何类型的编程超级新手,javascript运气不佳。 index.html.erb $(function() { $(“.areas”).buttonset(); }); $(function() { $( “button, input:submit, a”, “.filter_form_button” ).button(); $( “a”, “.form_filter_button” ).click(function() { return false; }); }); :get, :id => ‘filter_form’ do %> “area-#{a.id}” %> <label for="area-“> 任何帮助都非常感谢!

State Machine Gem + Rails 4:未定义的方法`state_machine’for#

我已经安装了gem gem ‘state_machine’, :require => ‘state_machine/core’ 我捆绑了。 我已将列状态类型字符串迁移到相关表。 我的模型看起来像这样 belongs_to :user belongs_to :patient validates :patient_id, uniqueness: {scope: [:email]} state_machine :initial => :unseen do event :look do transition :unseen => :seen end end 当我跑 rails c Table.all 我在引用state_machine的模型页面中得到了以下错误 undefined method `state_machine’ for Table 我注释掉了模型页面的相关部分,删除了该表中之前创建的所有记录,取消注释了模型的state_machine部分,然后从rails c中我可以成功创建记录,但state的值为nil。 同样令人不安的是,当我尝试加载我的首页时,我正在使用Angular,它会进行API调用以检索返回500错误的记录。 控制台日志显示 NoMethodError (undefined method `state_machine’ for #<Class… 此人( state_machine gem与rails […]

循环通过状态 – 范围? 的Ruby / Rails

我希望能够根据当前状态循环遍历模型。 我为我的模型创建了相当简单的状态,如下所示: models/job.rb class Job < ActiveRecord::Base has_many :events, class_name: "JobEvent" STATES = %w[bids in_progress complete canceled] delegate :bids?, :in_progress?, :complete?, :canceled?, to: :current_state def current_state (events.last.try(:state) || STATES.first).inquiry end end 和 models/job_event.rb class JobEvent < ActiveRecord::Base belongs_to :job attr_accessible :state validates_presence_of :job_id validates_inclusion_of :state, in: Order::STATES def self.with_last_state(state) order("id desc").group("job_id").having(state: state) end end 国家工作 […]

我应该如何避免在Ruby中导致错误的memoization?

关于如何避免由于可变状态导致错误的memoization,是否有共识? 在此示例中,缓存的结果的状态发生了突变,因此在第二次调用时会产生错误的结果。 class Greeter def initialize @greeting_cache = {} end def expensive_greeting_calculation(formality) case formality when :casual then “Hi” when :formal then “Hello” end end def greeting(formality) unless @greeting_cache.has_key?(formality) @greeting_cache[formality] = expensive_greeting_calculation(formality) end @greeting_cache[formality] end end def memoization_mutator greeter = Greeter.new first_person = “Bob” # Mildly contrived in this case, # but you could encounter this […]

ruby at_exit退出状态

我可以在at_exit块中确定自我进程退出状态吗? at_exit do if this_process_status.success? print ‘Success’ else print ‘Failure’ end end