Tuesday, 15 July 2014

erb - Can't get my dynamic code to be interpolated correctly in rails4 embeded ruby -



erb - Can't get my dynamic code to be interpolated correctly in rails4 embeded ruby -

i passing selected directory dropdown box , passing directory path sec dropdown box in partial plan have value interpolated , run dir.glob command correctly display available files in directory selected.

i having problem getting interpolation work correctly. 1 way utilize #{} variable in it. style works in controller have problem passing array of values partial (new rails , still learning can do).

i tried erb in partial think might have worked got error , list did not display: cannot convert nil string , on interpretation of select box value on form post page. method 'evaluate_media renders 'index' post.

here partial erb in it:

require 'erb' <p> <label>select partial test file:</label><br /> <label>dir partial selected path choice: </label><%= dir_path_choice %><br /> <%= dir_path_choice = params[:dir_list] %> <label>partial path choice: </label><%= @dir_path_choice %><br /> <% if dir_path_choice %> <% @dir = 'dir.glob("' << @dir_path_choice << '/**/*.{mpg,mov}").map' %> <% else %> <% dir = 'dir.glob('"/watchfolder/miniprod/hot/**/*.{mpg,mov}"').map' %> <% end %> <label>partial dir: </label><%= @dir %><br /> <% template = erb.new dir %> <% @files = template.result %> <%= select_tag 'filepath', options_for_select(@files, @selected_filepath) %> </p>

here string assignment works hard coded:

<% @files = dir.glob("/watchfolder/showtimevod/**/*.{mpg,mov}").map %> <%= select_tag 'filepath', options_for_select(@files, @selected_filepath) %>

here total partial works hard code , in display shows 2 values right in values: @dir_path_choice (contains directory path passed. @dir (is total dir.glob string executed.)

<p> <label>select partial test file:</label><br /> <label>dir partial selected path choice: </label><%= dir_path_choice %><br /> <%= @dir_path_choice = params[:dir_list] %> <label>partial path choice: </label><%= @dir_path_choice %><br /> <% if @dir_path_choice %> <% @dir = 'dir.glob("' << @dir_path_choice << '/**/*.{mpg,mov}").map' %> <% else %> <% @dir = 'dir.glob('"/watchfolder/miniprod/hot/**/*.{mpg,mov}"').map' %> <% end %> <label>partial dir: </label><%= @dir %><br /> <% @files = dir.glob("/watchfolder/showtimevod/**/*.{mpg,mov}").map %> <%= select_tag 'filepath', options_for_select(@files, @selected_filepath) %> </p>

here controller redering partial value:

def file_dir @dir_path_choice = params[:dir_list] # @files = "#{@dir}" # @files = "dir.glob(#{@dir_path_choice}/**/*.{mpg,mov}).map" # render :partial => 'list_files', :collection => @files, :as :item render :partial => 'list_files', :locals => {:dir_path_choice => @dir_path_choice } end end

require 'erb'

you don't require erb in views or partials. erb default template language rails.

in partial, trying access params hash:

<%= dir_path_choice = params[:dir_list] %>

i didn't know that, when tried it, able access params hash in partial. don't know why isn't working you, in case not considered practice.

it can helpful add together next line views/layouts/application.html.erb file:

<%= debug(params) if rails.env.development? %>

that way every page show what's in params hash. can set right before closing body tag. if want, can style debug output css:

.debug_dump { //rails automatically wraps debug info in <pre> tag class="debug_dump" clear: both; float: left; width: 100%; margin-top: 45px; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }

…which can set in assets/stylesheets/custom.css.

then load page giving problem , @ debug info see if dir_list has value.

if there value params[:dir_list], can seek couple of things:

you can seek accessing @dir_path_choice straight in partial. not considered practice, either. because did this: :locals => {:dir_path_choice => @dir_path_choice}, there should local variable available in partial named dir_path_choice desired content.

if there isn't value params[:dir_list], have problem whatever form submitting action.

also, none of ruby code:

<%= dir_path_choice = params[:dir_list] %> <label>partial path choice: </label><%= @dir_path_choice %><br /> <% if dir_path_choice %> <% @dir = 'dir.glob("' << @dir_path_choice << '/**/*.{mpg,mov}").map' %> <% else %> <% dir = 'dir.glob('"/watchfolder/miniprod/hot/**/*.{mpg,mov}"').map' %> <% end %> <label>partial dir: </label><%= @dir %><br /> <% template = erb.new dir %> <% @files = template.result %>

...has generating html, , hence should not in view--it should in controller. in addition, when this:

template = erb.new dir

the argument erb.new() must string containing erb, e.g

"<div><%= greeting %></div>"

and can this:

require 'erb' str = "<div><%= greeting %></div>" greeting = "hello world" my_erb = erb.new(str) puts my_erb.result #=><div>hello world</div>

but why trying that?

your dir variable, mentioned above, needs erb code, set here:

dir = 'dir.glob('"/watchfolder/miniprod/hot/**/*.{mpg,mov}"').map

yet line has no erb in it. in fact, don't know is. if seek match quotes, get:

dir = 'dir.glob(' "/watchfolder/miniprod/hot/**/*.{mpg,mov}" ').map

the first 2 single quotes create 1 string; next 2 double quotes create string, , is: ').map? if had quotes matched up, string not have map() method. should have gotten 1,000 errors on line.

finally, when post error message, interpretation of error message irrelevant: people need see exact error message re-create , pasted, line numbers , file names. rails error messages pretty long, can start first 15 lines, , should quoted this:

users/7stud/rails_projects/sample_app2/app/controllers/static_pages_controller.rb:3: unterminated string meets end of file /users/7stud/rails_projects/sample_app2/app/controllers/static_pages_controller.rb:3: syntax error, unexpected end-of-input, expecting keyword_end

ruby-on-rails-4 erb string-interpolation

No comments:

Post a Comment