我發佈了一個關於如何在同一控制器中的操作之間共享變量的問題。我發現這個問答最主要的回答了我的問題,但仍然留下了這個提交中提出的未決問題。 這裏是鏈接的Q/AI發現可能會幫助別人:Same instance variable for all actions of a controllerRails4如何在before_filter中定義變量值或在其他UI過程方法中定義私有操作時定義變量值?
這裏是鏈接,我,我跟上面的鏈接回答最初的問題:In Rails4 How to assign variables in one controller action and use the value of it in another controller action
現在,我知道我可以使用before_filter並創建一個私有操作,然後可以從控制器中的各種操作中引用該操作,這對於已知的常量變量來說顯得非常重要,我如何在基於用戶操作改變值並根據用戶操作定義的特定情況下對其進行設置 在用戶交互操作中?
我有一個名爲'evaluate_media'的方法。此方法允許用戶從各種目錄路徑中選擇一個文件,然後在選定的視頻文件上運行媒體信息,並解析xml中的某些屬性(如比特率,aspect_ratio)並將值分配給動作中的變量。有沒有什麼辦法可以將這個動作中的查找變量和賦值變量與common_components這樣的其他動作共享,而不需要它是私有的,只是用於變量賦值?你能告訴我一個如何做到這一點的例子嗎?我確實嘗試了before_filter,它甚至沒有調用插入。它只是刷新頁面。在我收集這些信息到我的evaluate_media動作之後,我有一個保存按鈕,調用save_file方法來存儲這個信息在evaluate_media操作中收集的信息。
這裏有2種定義的方法:
def save_file
src_location = @radio_button_value
directory = "#{@dir_path_choice}"
full_path = @filepathname
full_filename = "#{@filepath}"
alias_code = "#{@file_alias}"
validate_status = "#{@file_status}"
error_msg = "#{@file_msg}"
video_alias_match = "#{@msg_dtl1}"
audio_alias_match = "#{@msg_dtl2}"
video_format = "#{@video_format}"
video_bitrate = "#{@video_bitrate}"
video_width = "#{@video_width}"
video_height = "#{@video_height}"
video_framerate = "#{@video_framerate}"
video_aspect_ratio = "#{@video_aspectratio}"
video_scan_type = "#{@video_scantype}"
video_scan_order = "#{@video_scanorder}"
@file_alias_tfile = FileAliasTfile.new(:src_location => src_location, :directory => directory, :full_path => full_path, :full_filename => full_filename, :file_ext => '',
:assigned_status => 'Unassigned', :file_status => 'Saved', :alias_code => alias_code, :validate_status => validate_status, :error_msg => error_msg,
:video_alias_match => video_alias_match, :audio_alias_match => audio_alias_match, :video_format => video_format, :video_bitrate => video_bitrate,
:video_width => video_width, :video_height => video_height, :video_framerate => video_framerate, :video_aspect_ratio => video_aspect_ratio,
:video_scan_type => video_scan_type, :video_scan_order => video_scan_order, :video_alias_code => '', :audio_alias_code => '',
:bus_prod_initiative_id => 0, :status => 'Active', :start_date => DateTime.now.to_date, :end_date => '', :deleted_b => 0,
:created_by => 'admin', :updated_by => 'admin')
if @file_alias_tfile.save
redirect_to mainpages_home_path, :notice => "The file alias validation has been saved."
else
redirect_to alias_mainpages_home_path, :notice => "The file alias validation has not been saved."
end
end
def evaluate_media
@state = 'post'
@radio_button_value = params[:location]
@stored_file = FileAliasTfile.where(:full_path => params[:filepath2], :deleted_b => 0).first
if @stored_file.present?
@file_exists_flag = 'Y'
@file_exists_msg = 'This File and Alias has been saved in application.'
else
@file_exists_flag = 'N'
@file_exists_msg = 'This File and Alias has NOT been saved in application yet.'
end
root_dir = '/watchfolder/miniprod/hot/'
provider_dir = ""
@selected_filepath = params[:filepath2]
@filepath = params[:filepath2]
@media_xml = ::MediaInfo.call(@filepath)
@alias_xml = ::AliasGenerator.call(@media_xml)
@media_xml_for = ""
@alias_xml_for = ""
REXML::Document.new(@media_xml).write(@media_xml_for, 1)
REXML::Document.new(@alias_xml).write(@alias_xml_for, 1)
alias_parse_doc = ""
media_parse_doc = ""
alias_parse_doc = REXML::Document.new(@alias_xml)
media_parse_doc = REXML::Document.new(@media_xml)
#parse Alias XML Doc
@aliasgen_ver = REXML::XPath.each(alias_parse_doc, "/aliasGenerator vr=/text()") { |element| element }
@file_alias = REXML::XPath.each(alias_parse_doc, "*//alias/text()") { |element| element }
@file_status = REXML::XPath.each(alias_parse_doc, "*//error/text()") { |element| element }
@file_msg = REXML::XPath.each(alias_parse_doc, "*//error_m/text()") { |element| element }
@msg_dtl1 = REXML::XPath.each(alias_parse_doc, "*//closestvideoalias/text()") { |element| element }
@msg_dtl2 = REXML::XPath.each(alias_parse_doc, "*//closestaudioalias/text()") { |element| element }
#parse Video Media Info XML Doc
@filepathname = REXML::XPath.each(media_parse_doc, "*//Complete_name/text()") { |element| element }
@video_format = REXML::XPath.each(media_parse_doc, "//track[@type='Video']/Format/text()") { |element| element }
@video_bitrate = REXML::XPath.each(media_parse_doc, "//track[@type='Video']/Bit_rate/text()") { |element| element }
@video_width = REXML::XPath.each(media_parse_doc, "//track[@type='Video']/Width/text()") { |element| element }
@video_height = REXML::XPath.each(media_parse_doc, "//track[@type='Video']/Height/text()") { |element| element }
@video_aspectratio = REXML::XPath.each(media_parse_doc, "//track[@type='Video']/Display_aspect_ratio/text()") { |element| element }
@video_framerate = REXML::XPath.each(media_parse_doc, "//track[@type='Video']/Frame_rate/text()") { |element| element }
@video_scantype = REXML::XPath.each(media_parse_doc, "//track[@type='Video']/Scan_type/text()") { |element| element }
@video_scanorder = REXML::XPath.each(media_parse_doc, "//track[@type='Video']/Scan_order/text()") { |element| element }
render :action => :index
end