link_to passing query parameter - ruby-on-rails-4

Need to be able to generate the following URL string
http://localhost:3000/admin/cities?q%5Bprovince_id_eq%5D=1&commit=Filter&order=city_name_asc
how does this link_to need to be setup?
link_to(p.cities.count, admin_cities_path)

You could just pass the query parameters as a hash to the URL helper, e.g. Running the following commands in my console, I get the following hash:
url = "http://localhost:3000/admin/cities?q%5Bprovince_id_eq%5D=1&commit=Filter&order=city_name_asc"
query = URI.parse(url).query
hash = Rack::Utils.parse_nested_query(query)
#=> { "q" => { "province_id_eq" => "1" }, "commit" => "Filter", "order" => "city_name_asc" }
Then you'd just do
admin_cities_url(hash)
To get back to the original URL.

Probably this will help you, take a look after the "link_to can also produce links with anchors or query strings"
link_to(p.cities.count, admin_cities_path(q: { province_id_eq: 1 }, order: "city_name_asc"))

Related

How to update values dynamically for the individual match sections within sshd config file using puppet

i am able to update the value to the sections "User foo" and "Host *.example.net" by passing the index. If i pass index 1 or 2 the respective value is getting updated.
my code:
$sections = ['Host *.example.net', 'User foo']
$sections.each |String $section| {
sshd_config_match { "${section}":
ensure => present,
}
}
$settings = [['User foo', 'X11Forwarding yes', 'banner none'],['Host *.example.net', 'X11Forwarding no', 'banner none']]
$settings.each |Array $setting| {
$setting_array = split($setting[1],/ /)
sshd_config { "${setting_array[0]} ${setting[0]}":
ensure => present,
key => "${setting_array[0]}",
condition => "${setting[0]}",
value => "${setting_array[1]}",
}
}
current result:
Match Host *.example.net
# Created by Puppet
X11Forwarding no
Match User foo
# Created by Puppet
X11Forwarding yes
Expected results:
Match Host *.example.net
# Created by Puppet
X11Forwarding no
Banner none
Match User foo
# Created by Puppet
X11Forwarding yes
Banner none
i am able to update only one value mentioned in the index but am looking a way to update more or all the values mentioned in the list.
It's not clear what module is providing your sshd_config_match and sshd_config resource types, nor, therefore, exactly what they do. Nevertheless, if we consider this code ...
$settings = [['User foo', 'X11Forwarding yes', 'banner none'],['Host *.example.net', 'X11Forwarding no', 'banner none']]
$settings.each |Array $setting| {
$setting_array = split($setting[1],/ /)
sshd_config { "${setting_array[0]} ${setting[0]}":
ensure => present,
key => "${setting_array[0]}",
condition => "${setting[0]}",
value => "${setting_array[1]}",
}
}
... we can see that each element of $settings is a three-element array, of which the each call accesses only those at indexes 0 and 1. That seems to match up with the result you see, which does not contain anything corresponding to the data from the elements at index 2.
You could iterate over the inner $setting elements, starting at index 1, instead of considering that element only, but I would suggest instead restructuring the data more naturally, and writing code suited to the restructured data. You have data of mixed significance in your arrays, and you are needlessly jamming keys and values together such that you need to spend effort to break them back apart. Structuring the data as a hash of hashes instead of an array of arrays could be a good start:
$settings = {
'User foo' => { 'X11Forwarding' => 'yes', 'banner' => 'none'},
'Host *.example.net' => { 'X11Forwarding' => 'no', 'banner' => 'none'},
}
Not only does that give you much enhanced readability (mostly from formatting), but it also affords much greater usability. To wit, although I'm guessing a bit here, you should be able to do something similar to the following:
$settings.each |String $condition, Hash $properties| {
$properties.each |String $key, String $value| {
sshd_config { "${condition} ${key}":
ensure => 'present',
condition => $condition,
key => $key,
value => $value,
}
}
}
Again, greater readability, this time largely from a helpful choice of names, and along with it greater clarity that something like this is in fact the right structure for the code (supposing that I have correctly inferred enough about the types you are using).

Elixir: ExAws: DynamoDB: query-filter

I'm using the library https://github.com/ex-aws/ex_aws_dynamo, and I'm having issues getting a working example of query-filter to work with query. I was hoping someone here has an example they could share.
Here's what I've tried, but it returns an error:
[
key_condition_expression: "highlight_request_id = :highlight_request_id",
expression_attribute_values: [
highlight_request_id: "c692e65e-618f-45a3-ac12-d8103e6444c8"
],
query_filter: %{
range_id: %{
attribute_value_list: ["9990-ORGANIZATION-Pampers"],
comparison_operator: "EQ"
}
}
]
and the error I get back:
{:error,
{"ValidationException",
"1 validation error detected: Value null at 'queryFilter.range_id.member.comparisonOperator' failed to satisfy constraint: Member must not be null"}}
I'm not sure what is considered to be null. Any thoughts?
Make sure your query is written like so (adapted from the ex_aws_dynamo tests):
ExAws.Dynamo.query("person", [
index_name: "email",
key_condition_expression: "#email = :email",
expression_attribute_names: %{"#email" => "email"},
expression_attribute_values: [email: "person#test.com", last_name: "Person"],
filter_expression: "last_name = :last_name"
]) |> ExAws.request()
I was able to run this successfully against my local table, you'll have to fill in the appropriate values for your model. In particular, you'll want to include the index_name, and use the filter_expression, rather than query_filter.
In your case, I believe you'd want something like
ExAws.Dynamo.query("person", [
index_name: "highlight_request_id", # assuming that's the name of the index
key_condition_expression: "#highlight_request_id = :highlight_request_id",
expression_attribute_names: %{"#highlight_request_id" => "highlight_request_id"},
expression_attribute_values: [highlight_request_id: "c692e65e-618f-45a3-ac12-d8103e6444c8", range_id: "9990-ORGANIZATION-Pampers"],
filter_expression: "range_id = :range_id"
]) |> ExAws.request()
Let me know if this gets you any closer - again, worked for me, using the latest version of ex_aws_dynamo (2.2.2, at the moment).

How to do wildcard search using structured prefix operator with AWS CloudSearch

I've currently migrating to the 2013 Cloudsearch API (from the 2011 API). Previously, I had been using a wildcard prefix with my searches, like this:
bq=(and 'first secon*')
My queries sometimes include facet options, which is why I use the boolean query syntax and not the simple version.
I've created a new cloudsearch instance using the 2013 engine and indexed it. The bq parameter is gone now, so I have to use the q parameter with the q.parser=structured parameter to get the same functionality. When I query with something like this:
q.parser=simple&q=first secon*
...I get back a load of results. But when I query with this:
q.parser=structured&q=(prefix 'first secon')
...I get no hits. I don't get an error, just no results found. Am I doing something wrong?
I've just realized that if I do a prefix search for the word firs with the 2013 API, the prefix search seems to be working. But if I have any more than a single term in the query e.g. first secon then the prefix search does not work. So how is this accomplished using the structured prefix operator?
You need to specify the prefix operator for each separate query term, eg:
q=(or (prefix 'firs') (prefix 'secon'))&q.parser=structured
If someones looking for JS code to solve this issue. What you need to do is split the user input on space, and store them in an array. The join the words you want to query back together with pipes.
var params = {
query: ''
};
//Check for spaces
let words = query.split(' ');
let chunks = [];
words.forEach(word => {
chunks.push(`${word}* | ${word}`);
})
params.query = chunks.join(' | ');
cloudsearch.search(params, function(err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});

Logstash can not handle multiple heterogeneous inputs

Let's say you have 2 very different types of logs such as FORTINET and NetASQ logs and you want:
grok FORTINET using a regex, ang grok NETASQ using an other regex.
I know that with "type"in the input file and "condition" in the filter we can resolve this problem.
So I used this confing file to do it :
input {
file {
type => "FORTINET"
path => "/fortinet/*.log"
sincedb_path=>"/logstash-autre_version/var/.sincedb"
start_position => 'beginning'
}
file {
type => "NETASQ"
path => "/home/netasq/*.log"
}
}
filter {
if [type] == "FORTINET" {
grok {
patterns_dir => "/logstash-autre_version/patterns"
match => [
"message" , "%{FORTINET}"
]
tag_on_failure => [ "failure_grok_exemple" ]
break_on_match => false
}
}
if [type] == "NETASQ" {
# .......
}
}
output {
elasticsearch {
cluster => "logstash"
}
}
And i'm getting this error :
Got error to send bulk of actions: no method 'type' for arguments(org.jruby.RubyArray) on Java::OrgElasticsearchActionIndex::IndexRequest {:level=>:error}
But if don't use "type" and i grok only FORTINET logs it wroks.
What should i do ?
I'm not sure about this but maybe it helps:
I have the same error and I think that it is caused by the use of these if statements:
if [type] == "FORTINET"
your type field is compared to "FORTINET" but this is maybe not possible because "FORTINET" is a string and type isn't. Some times by setting a type to an input, if there is already a type, the type isn't replaced, but the new type is added to a list with the old type. You should have a look to your data in kibana (or wherever) and try to find something like this:
\"type\":[\"FORTINET\",\"some-other-type\"]
maybe also without all those \" .
If you find something like this try not to set the type of your input explicitly and compare the type in your if-statement to the some-other-type you have found.
Hope this works (I'm working with more complex inputs/forwarders and for me it doesn't, but it is worth a try)

mongo shell search for value like "/"fubak#drooop.com"/"

I may have let a bug loose on our code that essentially ruined some of our db data.
Every now and again we come across a username that has " " in his email so we need to fix this.
How can I search for a value like "/"fubak#drooop.com"/" in one field using my mongo shell?
I have been trying all the combinations I can think of like:
db.users.find(username: /"/" .* /)
and
db.users.find(username: //.*//)
or
db.users.find(username: /"".*/)
But nothing seems to work.
new RegExp('^".*')
This finds all the records that start with ".
Thanks.
Try this:
db.users.find({ email: "(?:[^\\"]+|\\.)* })
This will match all documents which contains quotes in the email
Not to sure which one is you problem, so the solutions to both possible scenarios with data in MongoDB like this:
{ "email" : "\"/\"fubak#drooop.com\"/\"" }
{ "email" : "/fubak#drooop.com/" }
{ "email": "\"blablabla#blip.ca\"" }
First document match:
db.problem.find({ email: /^\"/ })
Second document match:
db.problem.find({ email: /^\// })
Third document match
db.problem.find({ email: /^\"/ })
Do not know what you mean otherwise by "having quotes" as that is not valid JSON and by extension is also invalid BSON.