Why is this erb code throwing "Undefined method 'match' for []:Array"? - regex

Hope someone can help me resolve this problem.
I have an instance variable holding some string values in a params. Here are the string values:
"The Hoboken Four" "Anita Baker" "No One Cares" "Giant" "Taking A Chance On Love" "[]" "["quiz"]" "quiz"
I would like to display only the values that match:
"The Hoboken Four" "Anita Baker" "No One Cares" "Giant" "Taking A Chance On Love"
Here is the code in my app.rb:
post '/:form_type' do
#array = []
#results = params
#results.each do |key, value|
if value.match(/\w[^["quiz"]]/)
#array << value
end
#array
end
erb :results
end
Here is the code I have in my view:
<% #array.each do |item| %>
<p><%= item %></p>
<% end %>
Thank you for helping!

Just for the record, what I think happened is that OP was using [] in the parameters name, most likely to send all the values under a unique name. For instance if the HTML form looks like that:
<input type="text" name="title[]"/>
<input type="text" name="title[]"/>
and the user inputs First and Second, Sinatra is going to interpret the [] in the parameter name and aggregate the different values in a table:
{"title"=>["First", "Second"]}
So in OP's code, value.match was throwing because value was of type Array (as the error message states).

Related

How to render img with Phoenix?

I'm new in phoenix. I need to display all the posts and pictures to them on the main page. I written below code in my index template:
<img src="<%= Blog.Image.url({#post.image, #post}, signed: true) %>">
And i got this
lib/blog_web/templates/post/index.html.heex:19:21: expected closing " for attribute value
What i missing?
The HEEx documentation notes:
...code interpolation using <%= ... %> and <% ... %> are restricted to the body (inner content) of the HTML/component nodes and it cannot be applied within tags.
Instead, there is specific HEEx syntax attribute={expression} for interpolating attribute values. In your example, you'd use
<img src={Blog.Image.url({#post.image, #post}, signed: true)}>
The engine will correctly insert quotation marks and other escaping as required (or, if Blog.Image.url/2 returns nil, omit the attribute entirely).

Dancer2 IF statements in template

I'm working on a page that has a dropdown menu that gets populated from a database query. I also have an incoming parameter that, if it's populated, will match one of the options in the dropdown. If that parameter is populated, I want the option in the drowpdown to be selected. My instinct is to do something like the following (where foo is an entry in a FOREACH):
<% IF param -eq foo %>
<option value="foo" selected="selected">foo</option>
<% ELSE %>
<option value="foo">foo</option>
<% END %>
The problem is that I get an error that foo is an unrecognized token.
I can't find any evidence in documentation or examples that I can actually use the param -eq foo construct above. The examples I can find of IF statements don't use comparative operators. They basically only read a boolean parameter or check that a parameter has a value. But since it doesn't reject the -eq token, I've been holding out hope that there's a way to do it and that I just haven't figured out the right syntax or found the documentation yet.
Found the pertinent documentation: http://template-toolkit.org/docs/manual/Directives.html#section_IF_UNLESS_ELSIF_ELSE
It's not -eq, it's ==. But that being the case, I'm still not sure why -eq didn't throw an error.

Need help passing dynamic list of files to partial. Can't get list to pass successfully in Rails

In my application/index, the user selects a location and directory path from a drop down list. onchange event makes a call to the controller called file_dir where it takes the path, executes a command line call passing back a list of files in that directory to a parameter #files. I then render a partial passing this #files to a local variable. Then in the partial the select tag will display with the list of file passed to it.
I am new to ROR and have not been able to successfully pass a readable local variable. to the partial. Everything works fine with a hardcoded line but not with the local variable.
Can someone please help advise me on the correct way to set this up?
Here is the method called from the first onchange dropdown pass that receives the directory:
def file_dir
unless params[:dir_list].nil?
#dir_path_choice = params[:dir_list]
else
#dir_path_choice = '/watchfolder/indemandvod'
end
# #files = "#{#dir}"
#files = Dir.glob("#{#dir_path_choice}/**/*.{mpg,mov}").map
if #files.nil?
#files = Dir.glob("/watchfolder/hbovod/**/*.{mpg,mov}").map
end
render :partial => 'list_files', :locals => { #list => #files }
end
In irb I tested the #files = Dir.glob("#{#dir_path_choice}/**/*.{mpg,mov}").map line to make sure this was processing correctly. Here is what #files looks like:
1.9.3-p547 :008 > #files
=> #<Enumerator: ["/watchfolder/indemandvod/MJR-TEST.mov", "/watchfolder/indemandvod/MJR-TEST-AWS1.mov", "/watchfolder/indemandvod/MJR-TEST-AWS.mov", "/watchfolder/indemandvod/MJR-TEST2.mov", "/watchfolder/indemandvod/PIX_Gor_SVO40185/PIX_Gor_SVO40185_mezz.mov"]:map>
This data breaks out in the select list, separated by the commas.
NOTE: I did try to pass the #list without the # symbol but I got unidentified local variable or method and couldn't get the code to run with out adding the #. This is like a collection of data so I expect it is interpreting it as an array. Not sure though.
Here is the partial file code:
<p>
<label>Select Partial Test File List:</label><%= #list %><br />
<% unless #list.nil? %>
<%= #list %>
<% else %>
<label> list is empty. </label>
<% #list = Dir.glob("/watchfolder/showtimevod/**/*.{mpg,mov}").map %>
<% end %>
<%= select_tag 'filepath', options_for_select(#list, #selected_filepath) %>
</p>
It does display back on the page and my 'list is empty' always shows and the dropdown box populates with my default 'hardcoded' command line for '/watchfolder/showtimevod/' files list.
The #selected_filepath maintains the selected line in the list.
I don't know what I am doing wrong in passing values to partials.
In my application/index,
What is an "application/index"?
Can someone please help advise me on the correct way to set this up?
The correct way is to get something simple to work first:
app/controllers/some_controller.rb
def file_dir
puts "****#{params[:dir_list]}"
#files = %w[ a b c]
render :partial => 'list_files', :locals => { file_names => #files }
end
views/some_controller/_list_files.htm.erb:
<div>
<%= file_names.each do |fname| %>
<div><%= fname %></div>
<% end %>
</div>
And as with all your posts, in this line:
<%= select_tag(
'filepath',
options_for_select(#list, #selected_filepath) %>
#selected_filepath is undefined.
This is like a collection of data so I expect it is interpreting it as
an array. Not sure though.
You should probably strive to create data that you understand. What are you trying to accomplish with this line:
#files = Dir.glob("#{#dir_path_choice}/**/*.{mpg,mov}").map
that the following line doesn't do:
#files = Dir.glob("#{#dir_path_choice}/**/*.{mpg,mov}")
This:
I did try to pass the #list without the # symbol but I got
unidentified local variable or method and couldn't get the code to run
with out adding the #.
is not sufficient. No one cares what your interpretation of the error message is--what people care about is the EXACT error message, with file names and line numbers, copy and pasted into your post, along with the exact code the error refers to.
If you just want to get your code to work, you can do this:
app/some_controller.rb:
def file_dir
if params[:dir_list].nil?
#dir_path_choice = '/watchfolder/indemandvod'
else
#dir_path_choice = params[:dir_list]
end
#files = Dir.glob("#{#dir_path_choice}/**/*.{mpg,mov}").map
if #files.nil?
#files = Dir.glob("/watchfolder/hbovod/**/*.{mpg,mov}").map
end
render :partial => 'list_files', :locals => {file_names => #files }
end
However, adding map() to the end of your Dir.glob() does nothing useful.
app/views/some_controller/_list_files.html.erb:
<p>
<label>Select Partial Test File List:</label>
<%= select_tag 'filepath', options_for_select(file_names) %>
</p>

Dropdown Menu Populated by Database

Currently I have a model Abc. It has a column in it's associated database called 'description'. I'm trying to figure out how to populate a dropdown menu with the contents of the description so that the user would be able to select through each of the choices and pick one.
Currently, my action in my controller has this:
#abc = Abc.all
As far as I understand, this should select all records in the database that is associated with the model Abc.
Then, in the view associated with this action, I have this:
<select name="id">
<%= #abc.each do |abc|%>
<option value="<% abc.id %>"><%= abc.description%></option>
<% end %>
The above is something I was trying to adapt off an example I found online, which isn't working. I think this is supposed to create a dropdown menu with option values set to ID of the database entries. Then the actual content of the dropdown menu is populated with the contents of the 'description' column of the database. This isn't working as of now. Currently giving me this error:
undefined method `each' for nil:NilClass
on the:
<%= #abc.each do |abc|%>
line of code.
The example I was talking about is here:
<select name="user_id">
<%= #users.each do |user| %>
<option value="<%= user.id %>"><%= user.name %></option>
<% end %>
</select>
The context of the example is here: http://www.theodinproject.com/ruby-on-rails/advanced-forms
I'm pretty new to rails, any help would be appreciated. I get the feeling that I'm misunderstanding something about rails itself since the 'each' method is rails' version of an iterator.

How to insert URL in an HTML tag on Rails application

Ruby beginner developing for Rails 4.
I have a tag like:
<input type="hidden" name="URLholder" value=" ??? "/>
that I want to add my application root URL in the value attribute (instead of ???). How do I do that?
Note above that I need an input of type hidden
Thank you.
If you are using ERB templates:
# Remember to use double quotes for string interpolation
<%= text_field_tag 'URLholder', "#{root_url}" %>
For hidden input types, there is a FormTagHelper in Rails called hidden_field_tag,
# Remember to use double quotes for string interpolation
<%= hidden_field_tag 'URLholder', "#{root_url}" %>
Ref: http://apidock.com/rails/ActionView/Helpers/FormTagHelper/hidden_field_tag
Hope it works :)