そのため、コントローラー内の別の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)
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
または、これをこのビューに追加する必要がありますか?
(/views/awizard/_step1.html.erb)
<div class="field">
<%= f.label 'Step1' %><br />
#This is the step I want to validate
</div>