私はRubyを初めて使用しています。私は私のコントローラの下にコードがあります。 希望の動作: - 私は編集ページで私のビューに検証エラー文字列を表示したいと思います。だから私はこれらのエラーを変数に入れています。検証が失敗すると、その変数を編集メソッドで使用して、その変数を編集ページビューに表示できるようにします。Rubyの同じクラスのあるメソッドから別のメソッドへの変数の使用方法は?
観察された行動: - 変数@vpinerrはでありメソッドを作成します。私はその変数をの方法で使うことにします。私はクラス変数(@@ vpinerr)を使用しようとし、空の文字列(@@ vpinerr = "")で初期化しました。そして、この変数の値は編集メソッドで空になります。前に付ける@
と
require 'rho/rhocontroller'
require 'helpers/browser_helper'
class LeadController < Rho::RhoController
include BrowserHelper
# GET /Lead
def index
@leads = Lead.find(:all)
render :back => '/app'
end
# GET /Lead/new
def new
@lead = Lead.new
render :action => :new, :back => url_for(:action => :index)
end
def create
# Update
if Lead.find(@params['id'])
@lead = Lead.find(@params['id'])
# array of objects
@leadadd = LeadAddress.find(:all,
:conditions => {:parentKey => @lead.object}
)
@leadcon = LeadContact.find(:all,
:conditions => {:parentKey => @lead.object}
)
#hash of hashes
leadaddressArray = @params['leadaddress']
arr1 = @leadadd.count - 1
for i in 0..arr1
j=i.to_s
@leadaddHash = @leadadd[i]
leadaddressHash = leadaddressArray[j]
if leadaddressHash['removed'] == "1"
singleadd = LeadAddress.find(:first,
:conditions => {:object => leadaddressHash['object']}
)
singleadd.destroy if singleadd
else
#validation
vpin = leadaddressHash['pincode']
#validation check
if vpin =~ /^[[:digit:]]+$/
@leadaddHash.update_attributes(leadaddressHash) if @leadaddHash
else
err = 1
@vpinerr = "Pincode is invalid"
end
end
end
leadconArray = @params['leadcontact']
arr2 = @leadcon.count - 1
for k in 0..arr2
z=k.to_s
@leadconHash = @leadcon[k]
leadContact = leadconArray[z]
if leadContact['removed'] == "1"
singlecon = LeadContact.find(:first,
:conditions => {:object => leadContact['object']}
)
singlecon.destroy if singlecon
else
@leadconHash.update_attributes(leadContact) if @leadconHash
end
end
@lead.update_attributes(@params['lead']) if @lead
if err == 0
redirect :action => :index
else
redirect :action => :edit, :id => @lead.object, :vpin =>@vpinerr
end
else
# Create
err = 0
# validation
vlead = @params['lead']
vfirstname = vlead['firstname']
vlastname = vlead['lastname']
vage = vlead['age']
#validation check
if (vfirstname =~ /^[[:alpha:][:blank:]]+$/) and (vlastname =~ /^[[:alpha:][:blank:]]+$/) and (vage =~ /^[[:digit:]]+$/)
@lead = Lead.create(@params['lead'])
@key = @lead.object
else
err = 1
@basicerr = "Basic Details are invalid"
end
if @params['leadaddress']
leadaddressArray = @params['leadaddress']
arrcount = leadaddressArray.count
for i in 1..arrcount
j=(i-1).to_s
leadaddressHash = leadaddressArray[j]
#validation
vpin = leadaddressHash['pincode']
#validation check
if vpin =~ /^[[:digit:]]+$/
@leadAdd = LeadAddress.create(leadaddressHash)
@leadAdd.parentKey = @key
@leadAdd.save()
else
err = 1
@vpinerr = "Pincode is invalid"
end
end
end
if @params['leadcontact']
leadconArray = @params['leadcontact']
arrcount2 = leadconArray.count
for k in 1..arrcount2
h=(k-1).to_s
leadconHash = leadconArray[h]
#validation
vhome = leadconHash['home']
vmobile = leadconHash['mobile']
vemail = leadconHash['email']
#validation check
if (vhome =~ /^[[:digit:]]+$/) and (vmobile =~ /^[[:digit:]]+$/) and (vemail =~ /\A([\w+\-]\.?)[email protected][a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i)
@leadcontact = LeadContact.create(leadconHash)
@leadcontact.parentKey = @key
@leadcontact.save()
else
err = 1
@contacterr = "Contact Details are invalid"
end
end
end
if err == 0
redirect :action => :index
else
redirect :action => :edit, :id => @lead.object
end
end
end
# GET /Lead/{1}
def show
@lead = Lead.find(@params['id'])
@leadadd = LeadAddress.find(:all,
:conditions => {:parentKey => @lead.object}
)
@leadcontact = LeadContact.find(:all,
:conditions => {:parentKey => @lead.object}
)
if @lead
render :action => :show, :back => url_for(:action => :index)
else
redirect :action => :index
end
end
# GET /Lead/{1}/edit
def edit
@lead = Lead.find(@params['id'])
@leadaddress = LeadAddress.find(:all,
:conditions => {:parentKey => @lead.object}
)
@leadcontact = LeadContact.find(:all,
:conditions => {:parentKey => @lead.object}
)
@vpinerr2 = @vpinerr
if @lead
render :action => :new, :back => url_for(:action => :index)
else
redirect :action => :index
end
end
end
私たちはあなたを助けるための正確な十分なエラーの説明ではありません「渡していません」。 (実際、私はそれが何を意味するのかも分かりません)*何がうまくいかないのですか? *どのように動作しませんか?あなたのコードにはどんな問題がありますか?エラーメッセージが表示されますか?エラーメッセージとは何ですか?結果はあなたが期待している結果を得ていないのですか?あなたはどんな結果を期待していますか、そしてなぜ、あなたはどのような結果を得ていますか、そしてその二つの違いは何ですか?あなたが観察している行動は、望ましい行動ではありませんか?望ましい行動とは何か、なぜ行動が観察され、どのように違うのですか? –
[mcve](* Minimal *を中心に)を提供し、[ask]を読んでください。 –
投稿を編集しました。それは理解するのに十分ですか? –