使用Ruby on Rails 4,我有一个UI,用户可以从服务器上的目录路径中选择一个视频文件。然后,他们单击一个按钮,在该文件上运行媒体信息并显示视频的xml,同时解析xml中的某些数据值,并将值分配给@variables并将其显示回UI html页面。这一切都在动作/方法中完成,评估媒体'。当页面加载回来时,它确定是否已保存此文件,并允许用户将文件名,路径和媒体信息属性保存到mysql中的表。当您单击“保存”按钮时,它会调用acton / method' save_file'。当我尝试将这些变量值分配给模型类字段以插入表(在控制器内)时。不读取值。它只插入空值。怎么样 我可以在' evaluate_media'中分配这些变量值吗?行动也可用于' save_file'动作或同一控制器内的任何其他动作?根据它们的设置和定义方式,我认为它们可以在模型/控制器对象的范围内使用' file_alias_tfile'。
在' save_file'这里的行动是我尝试分配价值的3种不同方式:
full_path = params[:filepath2] <--This one puts a NULL into the table
src_location = @radio_button_value <--This one puts a NULL into the table
directory = "#{@dir_path_choice}" <--This one doesn't even get called into the insert script and nothing is inthe column on insert. (I don't get errors and a record does geet inserted with the other good values.)into the table
这是我的save_file操作/方法代码,我试图将我的变量值分配给模型字段:
def save_file
src_location = @radio_button_value
directory = "#{@dir_path_choice}"
full_path = params[:filepath2]
# "#{@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 been saved."
#render =&#34; index&#34; 结束 结束
这是&#39; evaluate_media&#39;在save_file中定义上面调用的值的方法:
def evaluate_media @state =&#39; post&#39; @radio_button_value = params [:location]
#Determine if file chosen has been saved to database yet.
@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 = ""
#Store selected value for re-display on post.
@selected_filepath = params[:filepath2]
@filepath = File.join(params[:filepath2])
@media_xml = ::MediaInfo.call(@filepath) #Filepath is sent in from the index html
@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 = alias_parse_doc.elements.each("/aliasGenerator vr=/text()") { |e| e }
@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 }
#parse Audio Media Info XML Doc
# @audio_track = REXML::XPath.each(media_parse_doc, "//track[@type= 'Audio']/track/text()") { |element| element }
# @track_array = REXML::XPath.each(media_parse_doc,(@audio_track)) do {|track| track.elements["Bit_rate"].text }
# @bitrate = track.elements["Bit_rate"].text
# end
#@audio_tracknum = REXML::XPath.each(media_parse_doc, "//track[@type='Audio']/track streamid=/text()") { |element| element }
# @audio_format = REXML::XPath.each(media_parse_doc, "//track[@type='Audio']/Format/text()") { |element| element }
# @audio_bitrate = REXML::XPath.each(media_parse_doc, "//track[@type='Audio']/Bit_rate/text()") { |element| element }
# @audio_numchan = REXML::XPath.each(media_parse_doc, "//track[@type='Audio']/Width/text()") { |element| element }
# @audio_language = REXML::XPath.each(media_parse_doc, "//track[@type='Audio']/Display_aspect_ratio/text()") { |element| element }
render :action => :index
端
您能否请我举例说明如何正确设置变量以传递操作和/或如何在插入数据库中正确分配它们?
感谢您的帮助!
答案 0 :(得分:0)
我在另一个问题中为我的搜索找到了答案,但它仍然留下了一个悬而未决的问题。
以下是其他问题和答案的链接以获取更多详细信息: Same instance variable for all actions of a controller
基本上它给了我两种方法。如果我的一个动作不是从另一个动作渲染,并且两个方法都需要访问这些值,那么我将需要定义另一个方法来分配变量,然后让save_file和evaluate_media方法调用这个新定义的方法,这样它们也可以访问变量。下面是一个示例,其中问题解决了索引并显示共享相同变量的方法。
def index
set_up_instance_variable
end
def show
set_up_instance_variable
end
private
def set_up_instance_variable
@some_instance_variable = foo
end
另一个解决方案是使用before_filter:
您可以使用前置过滤器为多个操作定义实例变量,例如:
class FooController < ApplicationController
before_filter :common_content, :only => [:index, :show]
def common_content
@some_instance_variable = :foo
end
end
现在可以从索引或show actions渲染的所有模板(包括部分模板)访问@some_instance_variable。