Fields_for, how to use this data in controller - ruby-on-rails-4.2

I'm lost onto how to use the data I get from using fields_for in rails. The array looks like this:
"order"=>{"order_item"=>{"1"=>{"quantity"=>"1"}, "2"=>{"quantity"=>"1"}}}
But I'm unsure on how to grab and use this data in a controller. For your information, I need to find the OrderItem by the id:
.. "order_item"=>{"1 (<-- this number is the id!)" .. and then update the quantity for that order_item.
I try to accept the params like this:
def order_params
params.require(:order).permit( order_item_attributes: [ :quantity ])
end
That should be correct, right?
in the controller I want to do something like this:
order_params[order_item_attributes].each do |item_id|
#orderitem = OderItem.find_by(id: item_id)
#orderitem.update(quantity: item_id[:quantity])
#... etc
I know the last code isn't correct, but I hope it helps you understand what I'm trying to do. I can find very little documentation on the fields_for and how to handle the data. Help would be welcome!
EDIT1:
This is what the view looks like
= form_for #order, :url => admin_update_order_path, :html => {:method => :post} do |f|
%table.detail_table
%tr
%th
%b Product
%th
%b Prijs
%th
%b Aantal
%th
%b Totaal
%th
%b Opties
- #order_items.each do |item|
%tr
%td
= item.product.name
%br
- x = 0
- item.product.product_attachments.each do |attachment|
-if item.product.id == attachment.product_id && x == 0
= image_tag attachment.image_url(:small)
- x = 1
%td= number_to_currency((item.product.price), :unit => "€", :separator => ",", :delimiter => ' ')
%td
= f.fields_for :order_items do |item_form|
= item_form.text_field :quantity, :class => "item_field"
%td= number_to_currency((item.product.price*item.quantity), :unit => "€", :separator => ",", :delimiter => ' ')
%td= link_to 'Verwijderen', item, method: :delete, data: { confirm: 'Weet u zeker dat u dit product uit dit order wilt verwijderen?' }
%br/
= f.submit
Thanks in advance

If you look at your params you are not passing in order_items_attributes you are passing order_item. Maybe if you change this you will have better luck.
The best way to test this is to fire up the rails console and copy your params into the console and see if you can get it working there. If you don't come right there then it wont work in the front end.
Also make sure that you have accepts_nested_attributes_for setup on your order model for order_items.
Edit 2
= f.fields_for item, index: item.id do |item_form|
= item_form.text_field
This above should be
= f.fields_for :order_items do |item_form|
= item_form.text_field
Your order model should have
has_many :order_items
accepts_nested_attributes_for :order_items
Your controller should permit params like so
def order_params
params.require(:order).permit( order_items_attributes: [:id :quantity])
end
Also if you want to get really funky with accepts_nested_attributes_for you should look into a gem called cocoon

Related

Extracting text based on a regex pattern with cheerio nodejs

I am trying to build a scraper with node.js which will allow me to extract news headlines from a large number of domains (they are all different so I have to be as general as possible in my approach). At the moment I have a working implementation in python which utilises Beautiful soup and regex allowing me to define a set of keywords and return headlines containing those keywords. Below is a relevant snippet of python code:
for items in soup(text=re.compile(r'\b(?:%s)\b' % '|'.join(keywords)))
To illustrate the expected output, lets assume there is a domain with news articles (Bellow is a html snippet containing a headline):
<a class="gs-c-promo-heading gs-o-faux-block-link__overlay-link gel-pica-bold nw-o-link-split__anchor" href="/news/uk-52773032"><h3 class="gs-c-promo-heading__title gel-pica-bold nw-o-link-split__text">Time to end Clap for Carers, says founder</h3></a>
The expected output given a keyword Time would be a string with a headline Time to end Clap for Carers
My question is: is it possible to do a similar thing with cheerio? What would be the best approach to achieve the same results in nodejs?
EDIT: This works for me now. On top of matching headlines I also wanted to extract post urls
function match_headlines($) {
const keywords = ['lockdown', 'quarantine'];
new RegExp('\\b[A-Z].*?' + "(" + test_keywords.join('|') + ")" +
'.*\\b', "g");
let matches = $('a').map((i, a) => {
let links = $(a).attr('href');
let match = $(a).text().match(regexPattern);
if (match !== null) {
let posts = {
headline: match['input'],
post_url: links
}
return posts
}
})
return matches.filter((x) => x !== null)
}
Maybe something like this:
let re = new RegExp('\\b' + keywords.join('|') + '\\b')
let texts = $('a h3').map((i, a) => $(a).text())
let titles = texts.filter(text => text.match(re))

Filter a column using IF statement in powerquery

I'm having trouble filtering my columns in powerquery. I'm using a parameter to filter my 'Island' NI or SI, however I am struggling to find a way to keep all the data when no parameter is inputted.
I would like to leave the column full (NI and SI) if no input is provided. I've added two pictures on imgur below. I'm fairly new here.
I've tried using an if statement but couldn't figure out the right piece of code or if it's even possible to do so.
Thanks
enter image description here. enter image description here
Note: see the very end for a version that does not need to hard-code values.
First let's test if the search filter is a valid value. If not, don't filter anything.
if IslandChoice is "NI" or "SI"
then filter using IslandChoice
else
show everything
The other route is
if IslandChoice is not "NI" and is not "SI"
show everything
else
filter using IslandChoice
It depends if you want invalid values to show nothing, or everything.
First check "is this filter a valid choice?
[Island] = "SI" or [Island] = "WI"
If either one is true, then it's good. If both are false, then it's an invalid key.
filter_if_valid = Table.SelectRows(
Source,
(row) =>
if IslandChoice = "SI" or IslandChoice = "NI" then
row[Island] = IslandChoice
else
true
)
How does it work, and where is each ?
each is a normal function. It's sugar, a shortcut that declares a function which has a single arugment. (If you need more, use a function declaration)
see more: Details on the grammar of each is in the docs
each implicitly creates the variable named _
Because each abstracts a couple of things, it's harder to tell what's happening.
each creates a variable named _ which references the current row.
each declares a function without the arguments part
Functions follow the form
(arguments) => return_expression
Written using each it removes the (arguments) => portion.
each return_expression
return_expression can be a simple [Column] = 10 test. (Or it can create many local variables using a let .. in expression inside the function).
These statements are all exactly equivelent functions
each [Island] = IslandChoice
each _[Island] = IslandChoice
(_) => _[Island] = IslandChoice
(row) => row[Island] = IslandChoice
They all
declare a function without a name
accept exactly 1 argument
read the column [Island] in the current table row
returns true if they are equal, else false
original filter
after replacing each you had:
always_filter_table = Table.SelectRows(
Source,
(row) =>
row[Island] = IslandChoice
),
new filter
filter_if_valid = Table.SelectRows(
Source,
(row) =>
if IslandChoice = "SI" or IslandChoice = "NI" then
row[Island] = IslandChoice
else
true
)
filter without hard-coding values
This will run alone without any requirements. Create a new -> blank query
let
Source = #table(
{"Island", "Point Of Connection"},
{
{"SI", "STK0331"},
{"SI", "TIM0111"},
{"NI", "ZEJ2395"},
{"NI", "XER9345"},
{"WI", "QXF9785"}
}
),
is_valid_filter = (value as any, valid_values as list) =>
List.Contains( valid_values, value ),
// use one or the other if you want all existing values to be valid
static_valid_filters = {"SI", "NI"},
dynamic_valid_filters = List.Distinct(
Source[Island]
),
IslandChoice = "NI",
// if filter is a valid value, filter using it.
// otherwise show all.
filtered_when_valid = Table.SelectRows(
Source,
(row) =>
if is_valid_filter(
IslandChoice, static_valid_filters
) then
row[Island] = IslandChoice
else
true
)
in
filtered_when_valid

aggregate anonymous types in linq

I have the below data.
var gradeData = (from data in oAngieCtxt.prc_ShopInstanceCustomersData(Convert.ToInt32(this.ShopInstanceID), 10000, false)
.Where(row => row.RecievedPoints != "n/a")
.GroupBy(row => new { row.Name })
.Select(g => new
{
TotalPoints = g.Sum(x => Convert.ToDouble(x.RecievedPoints) * (x.Weightage.ToString() == "0.00" ? 1 : Convert.ToDouble(x.Weightage))),
Name = g.Key.Name
})
select data).ToList();
I am assigning this to DataGrid as follows:
this.grdAllDealers.DataSource = gradeData;
this.grdAllDealers.DataBind();
This will result in screen as
5 A
10 B
15 C
.
.
.
.
However I want this data to printed on screen as
5 - A
10 - B
15 - C
.
.
.
.
hyphen should come in between TotalPoints and Name.
Can somebody advise how to get this in linq query?
Note: I know to get using foreach, but I want to do it without foreach.
Thanks in advance...
I fixed it and it is that simple... may will help future users like me ;)
this.grdAllDealers.DataSource = gradeData.Select(O=> O.TotalPoints + " - " + O.Name );

Drupal 7 - Ubercart - Attributes in Views Fields

I have products that have attributes for 'color' & 'strength'. I'm trying to get those options listed under those attributes as fields for views, so that I can use them as filters. So for example sort by color & strength.
I've looked all around on google and can only find modules for Drupal 6. Anyone know of anything for 7?
Like said in the previous post I had 2 attributes 'color' & 'strength' which I needed to match for exact product matches but ubercart didn't have anything for that, so I wrote them into URL for get statements as variables 1 & 2, so for example a URL which had both attributes selected would look like:
www.website.com/node/68?1=96&2=7
Sometimes one variable would be set but not the other, so I had to make up for that also by using a % wildcard. Here's the code for that part
// Since PHP's serialize function sometimes serializes in the incorrect order,
// here we manually build the comparison key
// Additionally append the image links urls with provided strength/color data
if( isset( $_REQUEST['1'] ) ) {
$_1 = $_REQUEST['1'];
if( $_1 !== '%' ) $url[] = "1=$_1";
} else $_1 = '%';
if( isset( $_REQUEST['2'] ) ) {
$_2 = $_REQUEST['2'];
if( $_2 !== '%' ) $url[] = "2=$_2";
} else $_2 = '%';
$combination = "a:2:{i:1;s:";
$combination .= $_1 == '%' ? '%:"%";' : strlen($_1) . ':"' . $_1 . '";';
$combination .= "i:2;s:";
$combination .= $_2 == '%' ? '%:"%";' : strlen($_2) . ':"' . $_2 . '";';
$combination .= '}';
// if some products don't have a second attribute at all
$combination2 = "a:1:{i:1;s:";
$combination2 .= $_1 == '%' ? '%:"%"' : strlen($_1) . ':"' . $_1 . '";';
$combination2 .= ';}';
Underneath that I had to do some extra queries like if a taxonomy was set and check if some dynamic properties were set, thats why the WHERE is stored in a variable. Frankly they'll just confuse anyone so I left them out. But for your sake the next part you just need to query it.
$where = "WHERE (pa.combination LIKE :pattern1 OR pa.combination LIKE :pattern2) AND s.stock IS NOT NULL";
$comparison = array( ':pattern1' => $combination,
':pattern2' => $combination2 );
$filtered = db_query(
"
SELECT pa.nid, pa.model, pa.combination, n.title, p.sell_price, f.uri
FROM {uc_product_adjustments} pa
LEFT JOIN {node} n ON pa.nid = n.nid
LEFT JOIN {uc_products} p ON pa.nid = p.nid
LEFT JOIN {field_data_uc_product_image} i ON i.entity_type = 'node' AND i.entity_id = n.nid
LEFT JOIN {file_managed} f ON f.fid = i.uc_product_image_fid
LEFT JOIN {uc_product_stock} s ON pa.model = s.sku AND s.stock <> '0'
$where
",
$comparison
);
Lastly loop over the results and store them in a normal array
foreach( $filtered as $i => $record ) {
if( is_int( array_search( $record->nid, $nids ) ) ) continue;
else {
$nids[] = $record->nid;
$result[] = $record;
}
}
This code checks for any products that match any of the attribute values that are currently in stock
You may have a look at these sandbox projects : UC Views Attributes Work and UC attribute views.
I'm looking for a similar feature, as it seems not possible to get UC attributes into a view. I cannot test myself right now as I have a deadline this weekend for a project, but I'd be happy to have your feedback.

Grails search mechanism

For my website, i need to do a search mechanism, in which some of the entry field would be: Country, City, Between Dates (with or without year field), Keywords, etc etc.
My problem is, the user must decide what they wanna search for. For example, if they want to introduce just date, or date and city, or city and keyword.. etc. I dont really know how to do that, i mean, i know how to search for one thing at a time, but i'm not sure how can do this all-in-one.
a) Would i need like something like this: (if-else, if-else) and than write the code for each combination, or there is an easier way to do that?
b )Bytheway, my search mechanism is done the folowing way (i'v never done a search mechanism before, so i dont know if it is the best aproach, would apreciate some comments here also and suggestions):
class book{
String a
String b
...
Date z
String allAttributesTogether() {
a + b + c + ... + z
}
}
then in my controller, i do a double for statment and cross-match the introduced words for the search and the result of allAttributesTogether().
Thanks in advanced, VA
Check out the filter pane plugin.
When you say "search", comes to my mind search engines. But I think you are asking about querying the database, right?
If you are talking about search mechanisms, search engines are a great tool. You can take a look at Lucene, Compass, and ElasticSearch (ES) to name a few. Compass and ES are based on lucene, but are much higher in the abstraction level (easier to use).
I have been using ElasticSearch with great satisfaction.
If you are talking about querying the database, then you can just build a HQL query dynamically. The method bellow should be in a Controller, as it uses the params attribute. It is not tested ok?
List allAttributesTogether() {
def query = " select book from Book book "
def queryParams = [:]
def needsAnd = false
if(params.a || params.b || params.z ){
query += " where "
}
if(params.a){
query += " book.a = :a "
queryParams['a'] = params.a
needsAnd = true
}
if(params.b){
if(needsAnd) query += " and "
query += " book.b = :b "
queryParams['b'] = params.b
needsAnd = true
}
if(params.a){
if(needsAnd) query += " and "
query += " book.z = :z "
queryParams['z'] = params.z
}
return Book.executeQuery(query, queryParams)
}
There is also the alternative of using Criteria builder. You can also use "if" to add clauses to your Criteria clauses.

Resources