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.
Related
I retrieved all the links from the web page containing /title/tt inside the url in a list.
my #url_links= $mech->find_all_links( url_regex => qr/title\/tt/i );
but the list is too long so I want to filter by adding in the function find_all_Links that the link must be also in the tags starting with <id="actor-tt..."> here is where the link (/title/tt...) is, in the code source retrieved by cmd.exe:
<div class="filmo-row odd" id="actor-tt0361748">
<span class="year_column">
2009
</span>
<b><a href="/title/tt0361748/"
>Inglourious Basterds</a></b>
<br/>
Lt. Aldo Raine
</div>
I imagine you have to use a tag_regex but I don't know how because the command prompt doesn't seem to take tag_regex into account when I put it in.
Using HTML::TreeBuilder and HTML::Element instead of Mechanize:
use strict;
use warnings;
use feature 'say';
use HTML::TreeBuilder;
my $html_string = join "", <DATA>;
my $tree = HTML::TreeBuilder->new_from_content($html_string);
my #url_links = map { $_->attr_get_i("href") }
map { $_->look_down(href => qr{/title/tt}) }
$tree->look_down(id => qr/^actor-tt/);
say for #url_links;
__DATA__
<div class="filmo-row odd" id="actor-tt0361748">
<span class="year_column">
2009
</span>
<b>Inglourious Basterds</b>
<br/>
Lt. Aldo Raine
</div>
<div id="not-the-right-id">
</div>
<div class="filmo-row odd" id="actor-tt0123456">
<b>Another movie</b>
</div>
<div class="filmo-row odd" id="actor-tt0123456">
the id will match, but no href in here
</div>
$tree->look_down(id => qr/^actor-tt/); finds all elements whose id matches actor-tt. Then $_->look_down(href => qr{/title/tt}) will find all elements within them with a field href matching /title/tt. Finally, $_->attr_get_i("href") returns the value of their href fields.
You might be interested in the method new_from_url or new_from_file from HTML::TreeBuilder rather than the new_from_content I used.
WWW::Mechanize is not sophisticated enough to do what you're trying to do. It can only search links on one criterium at a time, and it converts them to WWW::Mechanize::Link objects, which do not maintain their ancestry (as in position in the DOM tree).
Mechanize is meant to be a browser, not a scraper. It's important to pick the right tools for the job you have to do.
As Dada suggested in their answer, you can use your own parser to search for this. You can still extract the HTML out of WWW::Mechanize and then use the code they suggest. Use $mech->content or $mech->content_raw to get the HTML out.
There are several alternatives to this. While I personally like Web::Scraper for this kind of task, its interface is a bit weird and has a learning curve.
Instead, I would suggest using Mojo::UserAgent and Mojo::DOM. In fact, the handy ojo package for one-liners should be able to do this.
perl -Mojo -E 'g("https://www.imdb.com/name/nm0000093/")->dom->find("div[id^=actor-tt] a")->map(sub {say $_->attr("href")})'
Broken down, this does the following:
use Mojo::UserAgent to get that page
look at the DOM tree
find all <a>s inside <div>s that have an id that starts with actor-tt (see https://metacpan.org/pod/Mojo::DOM::CSS#SELECTORS for details)
for each of them, print out the href attribute
You can customise this as much as you want.
Please note that according to their Terms of Services, scraping IMDB is not allowed.
I am trying to submit a form, but if I just put form_for #classroom I get a "No route matches [POST]" error.
Now with the code posted below, I get the wrong url in the form. If I change the url manually in the browser it goes through, and I guess I could do that via javascript, but... why... is... this... happening..?
Until yesterday everything was working fine. I even tried rolling back to the things I changed but I can't seem to track what is going wrong.
routes.rb
patch 'classrooms/:id/update' => "classrooms#update", as: :update_classroom
resources :classrooms, except: :update
form from rails end
<%= form_for(update_classroom_path(#classroom), method: "patch") do |class_f| %>
form in browser
<form action="/classrooms/23/edit" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="authenticity_token" value="******">
rake routes
absences POST /absences(.:format) absences#create
POST /classrooms/:id/getAbsences(.:format) classrooms#getAbsences
update_classroom PATCH /classrooms/:id/update(.:format) classrooms#update
classrooms GET /classrooms(.:format) classrooms#index
POST /classrooms(.:format) classrooms#create
new_classroom GET /classrooms/new(.:format) classrooms#new
edit_classroom GET /classrooms/:id/edit(.:format) classrooms#edit
classroom GET /classrooms/:id(.:format) classrooms#show
DELETE /classrooms/:id(.:format) classrooms#destroy
root GET / pages#start
Just to answer your question from title, I think your form method is "PATCH" indeed. Refer to the guide http://guides.rubyonrails.org/form_helpers.html about how rails makes a patch form.
The Rails framework encourages RESTful design of your applications, which means you'll be making a lot of "PATCH" and "DELETE" requests (besides "GET" and "POST"). However, most browsers don't support methods other than "GET" and "POST" when it comes to submitting forms.
Rails works around this issue by emulating other methods over POST with a hidden input named "_method", which is set to reflect the desired method:
To add a bit of specificity, to Tashow's answer above (which set me on the right track), I had some hidden fields that look'd like this, in a nested form.:
<%= hidden_field_tag("Classroom[classroom_teachers_attributes][]", nil) %>
<%= hidden_field_tag("Classroom[classroom_teachers_attributes][]", '') %>
Once I got rid of these, everything began working properly again. (There still remained similar-looking <input> tags generated by fields_for, etc.)
After some more trial and error, I realised I had left some plain input tags in a deeper nested level of the form (instead of going with the normal fields_for and separate builders for each level). I guess that somehow screwed up the relations and affected the method of the parent form.
That was such a mind blending mess up.
Edit: Andylee's answer is right. What I and Jeremy mention was probably the actual issue going on and not what was originally assumed to be the problem (as mentioned in the title).
form_for takes an object as first argument, and its usually better to keep the REST-like way of rails handling the update method.
The action of you html form displays "/classrooms/23/edit" so yes it won't work.
form_for(#classroom, url: update_classroom_path(#classroom), method: "patch")
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).
I have a directory full of Classic ASP legacy code, and almost all files have something similar to this:
<input type="hidden" name="driverPayment" value="<% =driverPayment %>">
Then later in the code, some JavaScript is running, and doing the following:
var x = document.getElementById('driverPayment')
This works fine in IE, but of course doesn't work anywhere else because there is no ID attribute defined.
The fix is to go through the 1770 files and manually add an ID attribute that matches the name property on the input. So make it like so:
<input type="hidden" name="driverPayment" id="driverPayment" value="<% =driverPayment %>">
Is there a way I can automate this process by using the logic below:
Get input element with a name attribute
If input has id attribute, move to next input
Else add an ID attribute to the input, and give it a name matching the value of the name attribute
I'd like to do this for the 1770 Classic ASP files I have. I am using Sublime Text.
You can use regex to match. My regex isn't great but the following should work. Happy for others to improve on it. I used some regex from this question.
Right Click project folder
Choose "Find in folder" option
Find and replace options appear at bottom of screen. Select the regex option (far left).
Enter
<(?:input)\s+(?=[^>]*\bname\s*=)(?![^>]*\bid\s*=)[^>]*>?(name="driverPayment")
in Find field
Enter
id="driverPayment" name="driverPayment"
in Replace field
Click Replace
I'm using a pagination example from balbus design. In the .ss template, there is a line of code:
<% control ProductList.PaginationSummary(5) %>
Is that possible to use a variable instead of hard-coding the value 5? For example:
<% control ProductList.PaginationSummary(PSSize) %>
The variable PSSize is defined in the model and will return the number set in the CMS.
The SS 2.4 templating language is very limited in terms of what it can do.
In this specific case, you could try working it out in the controller - try adjusting the $resultSet within ProductListPage_Controller::ProductList to preprocess the pagination summary to desired context size, so you can access it later from the template.
Try something like that:
$resultSet->AdjustedPaginationSummary = $resultSet->PaginationSummary($this->productsPerPage);
return $resultSet;
And then in the template you should be able to do:
<% control ProductList.AdjustedPaginationSummary %>