【发布时间】:2014-12-05 19:34:46
【问题描述】:
我发布了一个关于如何在同一控制器中跨操作共享变量的问题。我发现这个 Q/A 主要回答了我的问题,但仍然在此提交中提出了一个悬而未决的问题。 这是我发现的可能对其他人有帮助的问答链接:Same instance variable for all actions of a controller
这是我最初用上面的链接回答的问题的链接: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 ,它甚至没有调用插入。它只是刷新页面。 before_filter :evaluate_media, :only => [:save_file]
就像我第一个问题的复习一样,在我在评估媒体操作中收集此信息后,我有一个保存按钮,该按钮调用 save_file 方法,该方法将存储在评估媒体操作中收集的此信息。
这里有两个定义的方法:
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
【问题讨论】:
标签: variables ruby-on-rails-4 model-view-controller