Jes*_*ham 5 forms validation model-view-controller ruby-on-rails activemodel
所以我有一些数据从控制器中的另一个rails应用程序中拉出来让我们称之为ExampleController,我想在我的模型中验证它,然后才允许向导进入下一步,我无法弄清楚如何我应该这样做(我知道直接从控制器获取这些数据到模型中违反了MVC我正在寻找从控制器获取数据的最佳解决方法).数据必须来自控制器,因为获取它的方法包含在ApplicationController中,但如果这更容易,我可以在Awizard控制器中执行此操作.(我也不能用宝石)
请提供一些问题的建议,而不是解释为什么这不是正确的做事方式我已经意识到已经但不能以另一种方式做到.
示例控制器
这应该代替渲染数据,然后检查它在其他地方是不是空白?
class ExampleController < ApplicationController
def valid_data?
data = #data could be nil or not
if data.blank?
return false
else
return true
end
end
Run Code Online (Sandbox Code Playgroud)
我的模型 - (models/awizard.rb)
我如何使用valid_data?来自示例控制器的方法?在我的验证中.
class AWizard
include ActiveModel::Validations
include ActiveModel::Conversion
include ActiveModel::Dirty
include ActiveModel::Naming
#This class is used to manage the wizard steps using ActiveModel (not ActiveRecord)
attr_accessor :id
attr_writer :current_step #used to write to current step
define_attribute_methods [:current_step] #used for marking change
validate :first_step_data, :if => lambda { |o| o.current_step == "step1" };
def first_step_data
#What should i put here to check the valid_data? from the examplecontroller
end
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def current_step
@current_step || steps.first
end
def steps
%w[step1 step2 step3] #make list of steps (partials)
end
def next_step
current_step_will_change! #mark changed when moving stepped
self.current_step = steps[steps.index(current_step)+1] unless last_step?
end
def previous_step
current_step_will_change! #mark changed when moving stepped
self.current_step = steps[steps.index(current_step)-1] unless first_step?
end
def first_step?
current_step == steps.first
end
def last_step?
current_step == steps.last
end
def all_valid?
steps.all? do |step|
self.current_step = step
valid?
end
end
def step(val)
current_step_will_change!
self.current_step = steps[val]
end
def persisted?
self.id == 1
end
end
Run Code Online (Sandbox Code Playgroud)
或者我需要将此添加到此视图中吗?
(/views/awizard/_step1.html.erb)
<div class="field">
<%= f.label 'Step1' %><br />
#This is the step I want to validate
</div>
Run Code Online (Sandbox Code Playgroud)
我可能误解了这个问题,因为我的答案很简单。然而,这里的解决方案不诉诸元编程,而是依靠 Wizard(它创建的类而不是对象)是单例/常量这一事实。
class ExampleController < ApplicationController
def valid_data?
data = #data could be nil or not
result = data.blank?
Awizard.valid_data= result
result
end
end
class Wizard
cattr_accessor :valid_data
def valid_data?
self.class.valid_data
end
end
Run Code Online (Sandbox Code Playgroud)
当然,在使用传递step_one的向导之前,必须先调用ExampleController#valid_data。
(由@Valery Kvon 提出)
争论的焦点是 Wizard 对于应用程序来说是全局的,@wizard 实例将依赖于全局状态,因此封装得很差。但是来自另一个站点的数据在您的应用程序范围内是全局的。因此,巫师作为持有数据的人并没有不匹配。相反,它可以被视为一个特征。
举一个例子。巫师的魔法只有在满月时才有效。应用程序 SkyReport 发送数据:
:full_moon => true
Run Code Online (Sandbox Code Playgroud)
如果他们需要继续其力量的第二步,它会影响第一阶段的所有巫师。因此,依赖全局状态Wizard.valid_data?
正是我们想要的......
但是,如果每个向导都有来自甘道夫应用程序的个人消息,那么我们将希望强制调用甘道夫的数据,但解决方案甚至更简单:
# in example_controller.rb
before_filter :set_wizard_data, :only => [:create, :update]
....
def set_wizard_data
@wizard = Wizard.find params[:id]
@wizard.valid_data= valid_data
end
Run Code Online (Sandbox Code Playgroud)
但这再次意味着 Gandalf.app 了解 @wizard(的一些情况),并且从问题的呈现方式来看,来自其他站点的数据是相当不可知的!
这里的问题是我们对应用程序、它的要求和底层逻辑了解不够,无法决定什么是好是坏......
归档时间: |
|
查看次数: |
3006 次 |
最近记录: |