我有一个这样的代码段:
class AwardunitsController < ApplicationController
include AwardunitsHelper
before_action :signed_in_admin #Only allow signed in admins to the defined actions
###########################
########## Note : There are single duplicate lines. Check for them
def index
############ Turn empty params[:district] to nil and params[:district] "Select District" to nil
params[:district] = nil_paramsdistrict(params[:district])
############ Turn empty @district to nil
@district = nil_district(@district)
############ Set the District. If params[:district] is not nil, @district = params[:district]. If params[:district] is nil and @district is not nil params[:district] = @district
## DUPE4
if params[ :district ] != nil
@district = params[ :district ]
elsif @district != nil
params[ :district ] = @district
end
end # Index action end ################################################################################################
def new
############ Turn empty params[:district] to nil and params[:district] "Select District" to nil
params[:district] = nil_paramsdistrict(params[:district])
############ Turn empty @district to nil
@district = nil_district(@district)
############ Set the District. If params[:district] is not nil, @district = params[:district]. If params[:district] is nil and @district is not nil params[:district] = @district
## DUPE4
if params[ :district ] != nil
@district = params[ :district ]
elsif @district != nil
params[ :district ] = @district
end
############ Create new Awardunit object
@awardunit = Awardunit.new
############ Set contact variables. *fie = number of fields. Array.new will create new empty Arrays to store contact numbers.
@contactfie = 1
@contacts = Array.new
@instheadcontactfie = 1
@instheadcontacts = Array.new
end # New action end ################################################################################################
def create
############ Turn empty params[:district] to nil and params[:district] "Select District" to nil
params[:awardunit][:district] = nil_paramsdistrict(params[:awardunit][:district])
############ Set @disabled value. @disabled value is used to set Disabled field's default selected value. If params[:awardunit][:disabled] is nil, set @disabled to 'No' otherwise @disabled = params[:awardunit][:disabled]
if params[:awardunit][:disabled] == nil
@disabled = 'No'
else
@disabled = params[:awardunit][:disabled]
end
############ Convert Disabled value from 'yes' 'no' to database accepted values 'true' and 'false'
if params[:awardunit][:disabled] == "Yes"
params[:awardunit][:disabled] = true
else
params[:awardunit][:disabled] = false
end
############ Set Province wording. If params[:awardunit][:district] is not set, make @province nil
if params[:awardunit][:district] != nil
if (params[:awardunit][:district] == "Kandy") || (params[:awardunit][:district] == "Matale") || (params[:awardunit][:district] == "Nuwara Eliya")
@province = "Central Province : (CP)"
elsif (params[:awardunit][:district] == "Ampara") || (params[:awardunit][:district] == "Batticaloa") || (params[:awardunit][:district] == "Trincomalee")
@province = "Eastern Province : (EP)"
elsif (params[:awardunit][:district] == "Anuradhapura") || (params[:awardunit][:district] == "Polonnaruwa")
@province = "North Central Province : (NCP)"
elsif (params[:awardunit][:district] == "Kurunegala") || (params[:awardunit][:district] == "Puttalam")
@province = "North Western Province : (NWP)"
elsif (params[:awardunit][:district] == "Vavuniya") || (params[:awardunit][:district] == "Mullaitivu") || (params[:awardunit][:district] == "Mannar") || (params[:awardunit][:district] == "Kilinochchi") || (params[:awardunit][:district] == "Jaffna")
@province = "Northern Province : (NP)"
elsif (params[:awardunit][:district] == "Ratnapura") || (params[:awardunit][:district] == "Kegalle")
@province = "Sabaragamuwa Province : (SGP)"
elsif (params[:awardunit][:district] == "Matara") || (params[:awardunit][:district] == "Hambantota") || (params[:awardunit][:district] == "Galle")
@province = "Southern Province : (SP)"
elsif (params[:awardunit][:district] == "Moneragala") || (params[:awardunit][:district] == "Badulla")
@province = "Uva Province : (UP)"
elsif (params[:awardunit][:district] == "Kalutara") || (params[:awardunit][:district] == "Gampaha") || (params[:awardunit][:district] == "Colombo")
@province = "Western Province : (WP)"
else
@province = nil
end
else
@province = nil
end
############ Set Province code for NYAAID
if @province != nil
@provincecode = @province.match(/(\((.*)\))/)[2]
end
############ Create new Awardunit using params
@awardunit = Awardunit.new(awardunit_params)
############ Build the NYAAID from multiple fields if params[:nyaaid] is not nil only and assign it to @awardunit
if params[:nyaaid] != nil
@nyaaids1 = params[:nyaaid][:nyaaids1]
@nyaaids2 = params[:nyaaid][:nyaaids2]
@nyaaid = "NYAA/#{@nyaaids1}/#{@provincecode}#{@nyaaids2}"
@awardunit.nyaaid = @nyaaid
end
############ Build contact array from multiple fields and assign it to @awardunit
@contactfie = params[:contactfie].to_i
@contacts = Array.new
(1..@contactfie.to_i).each do |i|
@contacts << params[:contact][:"#{'contact'}#{i}"]
end
@awardunit.contact = @contacts
############ Build Instaheadcontact array from multiple fields and assign it to @awardunit
@instheadcontactfie = params[:instheadcontactfie].to_i
@instheadcontacts = Array.new
(1..@instheadcontactfie.to_i).each do |i|
@instheadcontacts << params[:instheadcontact][:"#{'instheadcontact'}#{i}"]
end
@awardunit.instheadcontact = @instheadcontacts
############ Form submit actions
##################### Add / Remove contact numbers
if params[:commit] == 'More Numbers'
@contactfie = @contactfie + 1
render 'new'
elsif params[:commit] == 'Less Numbers'
if @contactfie > 1
@contactfie = @contactfie - 1
render 'new'
else
flash.now[:error] = "Can't remove all contact fields!"
render 'new'
end
##################### Add / Remove Instheadcontact numbers
elsif params[:commit] == 'More Head Numbers'
@instheadcontactfie = @instheadcontactfie + 1
render 'new'
elsif params[:commit] == 'Less Head Numbers'
if @instheadcontactfie > 1
@instheadcontactfie = @instheadcontactfie - 1
render 'new'
else
flash.now[:error] = "Can't remove all contact fields!"
render 'new'
end
##################### Add Awardunit
elsif params[ "commit" ] == "Add Award Unit"
if @awardunit.save
flash[ :success ] = "Award Unit #{@awardunit.name} Successfully Added!"
redirect_to new_awardunit_url
else
render 'new'
end
else
render 'new'
end
end #Create action end ################################################################################################
index
和create
是此控制器中的两个操作。代码已被复制。我怎样才能重新考虑这个因素?
答案 0 :(得分:2)
您可以在私有方法中提取代码并重复使用它:
def index
meaningful_name
end
def create
meaningful_name
end
private
def meaningful_name
if params[ :district ] != nil
@district = params[ :district ]
elsif @district != nil
params[ :district ] = @district
end
end
答案 1 :(得分:1)
您可以使用Controller的before_action
过滤器在定义的一组操作之前执行(私有)方法。
请参阅the documentation on filters
class MyController < ActionController::Base
before_action :meaningful_name, only: [:index, :create]
def index
# your normal index code
end
def create
# your normal create code
end
private
def meaningful_name
if params[ :district ] != nil
@district = params[ :district ]
elsif @district != nil
params[ :district ] = @district
end
end
end
答案 2 :(得分:0)
Ruby有方法来测试nil
但代码仍然闻起来
def index
meaningful_name
end
def create
meaningful_name
end
private
def meaningful_name
if !params[ :district ].nil?
@district = params[ :district ]
elsif @district.nil?
params[ :district ] = @district
end
end