所以我有一些数据是从控制器中的另一个 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
我的模型 - (models/awizard.rb)
如何使用有效数据?示例控制器中的方法?在我的验证中。
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
还是我需要将此添加到此视图中?
(/views/awizard/_step1.html.erb)
<div class="field">
<%= f.label 'Step1' %><br />
#This is the step I want to validate
</div>