Ai đó có thể vui lòng giúp tôi hiểu phương thức 'send()' được liệt kê dưới đây được sử dụng để làm gì? Đoạn mã dưới đây, khi tôi đọc nó, không có ý nghĩa gì về mục đích phục vụ.Rails 100% newb issue - phương thức send()
Đây là ứng dụng Rails sử dụng Ruby 1.8.7 với Rails 1.2.3. Xin vui lòng không harp về tôi về nâng cấp, đó là một môi trường của khách hàng, vì vậy tôi không có loại giải trí.
Không cần phải nói mặc dù, tuyên bố tôi đang đề cập đến là như thế này;
def do_schedule
@performance = Performance.new(params[:performance])
@performer = Performer.find(params[:performer_id])
selected_track = params[:selected_track]
if FileTest.exists?(File.expand_path(@performer.photo))
@performance.photo = File.open(File.expand_path(@performer.photo))
end
@performance.audio = File.open(File.expand_path(@performer.send(selected_track)))
if @performance.save
flash[:notice] = 'Performer scheduled.'
redirect_to :controller => :performer, :action => :index
else
render :action => 'schedule'
end
end
Performer Mẫu
class Performer < ActiveRecord::Base
file_column :audio_one
file_column :audio_two
file_column :audio_three
file_column :photo
belongs_to :festival
validates_presence_of :name, :first_name, :last_name, :address, :city, :state, :zip, :daytime_phone, :availability, :stages
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
validates_confirmation_of :email
validates_presence_of :audio_one, :audio_two, :audio_three, :photo, :if => :submitted
after_create :salt_access_key
serialize :availability
serialize :stages
attr_accessor :other_type_of_music
before_save :set_other_type
def set_other_type
if type_of_music == 'Other'
self.type_of_music = "Other - #{other_type_of_music}" unless other_type_of_music.blank?
end
end
def salt_access_key
update_attribute(:access_key, Digest::SHA1.hexdigest("--#{self.id}--#{self.name}--#{self.festival.year}"))
end
def preferred_stages
stages = []
festival = Festival.find(self.festival_id.to_i)
self.stages.collect { | key, value |
id = key.gsub(/[\D]/, '').to_i
if id > 0
stages << festival.performance_stages.find(id).name
end
}
return stages
end
end
Bộ điều khiển rằng đây được chứa trong là hiệu suất. Tôi đã cọ rửa Google cố gắng tìm ra mục đích mà '@ perform.send (selected_track)' thực sự đang làm, nhưng cảm thấy như tôi đang chèo thuyền với một xoáy nước.
Ai đó có thể giúp tôi hiểu rõ hơn về điều này không?
Cảm ơn.
Cảm ơn ctcherry! Ví dụ tuyệt vời! Đối với tham chiếu method_missing bạn vừa tạo. Không, thực ra chỉ có 4 phương thức trong lớp Performer. 3 (chỉ mục, hiển thị, và tiêu diệt) và tìm kiếm riêng tư_festival. Điều đó có nghĩa là việc gửi đó không có khả năng hoạt động đúng không? – Skittles
Gửi sẽ vẫn hoạt động. Những gì nó được kích hoạt phụ thuộc vào biến 'selected_track' đang được chuyển vào nó. Bạn có thể đăng nhập và hiển thị nó cho chúng tôi và chúng tôi có thể cung cấp cho bạn thêm thông tin. – ctcherry
Ngoài ra, bạn có đang tìm kiếm trong mô hình Trình điều khiển hoặc bộ điều khiển không? Bạn muốn xem mã mô hình cho sự cố gửi cuộc gọi này. – ctcherry