Elixir "no match of right hand side value" in controller Phoenix - match

I'm using the framework Phoenix with Ecto Mongo and I'm trying to get all Groups(My model) and loop this in view.
#groups = Group |> GlobalDocs.Repo.all
And I'm getting this message error:
no match of right hand side value: [%Group{__meta__: #Ecto.Schema.Metadata<:loaded>, avatar_url: nil, description: "", group_id: 123, id: "585ea2ce6e8dee0a6c04ecf6", name: "TryGroup", slug: "try_group"}]
Why this? If I run this in IEx, this code works.
Thanks for this.

Elixir and Phoenix do not work the same way as Ruby and Rails does.
When you use an # variable. They are called module attributes. In Phoenix, you do not use module attributes in order to have a variable accessible in a view like you would with rails.
In Phoenix, you would use something like the following.
groups = GlobalDocs.Repo.all(Group)
render conn, "index.html", groups: groups
Now, in your view, you will have access to a #groups variable.

Related

In a Quiz, how can i instead of "Say A, B or C" let the user use one of the three response words?

VIA Actions Console, not Dialogflow!
After several days I finally finished to create a Quiz that works like this.
Google Mini says: "What is the capital of France? A) Rome, B) Berlin or C) Paris ?"
In my scene i have two conditions.
scene.slots.status == "FINAL" && intent.params.choosenABC.original == session.params.antwort
AND
!(scene.slots.status == "FINAL" && intent.params.choosenABC.original == session.params.antwort)
So here, these conditions check whether the user says the correct letter coming from the session parameter "antwort".
Everything works smooth as long as the user says "A", "B" or "C".
But how can i compare a condition to what the user says?
In the above example i want the user to be able to say "Rome" or "Berlin" or "Paris" and the condition to check these entries.
Thanks in advance!
You have a number of questions packed in there, so let's look at each.
Does input.params.original exist?
In short, yes. You can see the documentation of the request Intent object and you'll see that there is intent.params.*name*.original. Your question seems to suggest this would work as well.
There is also intent.params.*name*.resolved which contains the value after you take type aliases into account.
I found some variables on a Dialogflow forum...
Those only work if you're using Dialogflow and don't make any sense when you're looking at Action Builder.
How to match
You don't show the possible value of session.params.antwort or how you're setting antwort. But it sounds like it makes sense that you're setting this in a handler. So one thing you could do is to set antwort to the city name (or whatever the full word answer is) and set letter to the letter with the valid reply. Then test both against original to see if there is a match.
But, to be honest, that starts getting somewhat messy.
You also don't indicate how the Intent is setup, or if you're using an Entity Type to capture the answer. One great way to handle this, however, is to create a Type that can represent the answers, and use a runtime type override to set what the possible values and aliases for that value are. Then, you can control exactly what the valid value you will use to compare with will be.
For example, if you create a type named "Answer", then in your fulfillment when you ask the question you can set the possible values for this with something like
conv.session.typeOverrides = [{
name: 'Answer',
mode: 'TYPE_REPLACE',
synonym: {
entries: [
{
name: 'A',
synonyms: ['A', 'Rome']
},
{
name: 'B',
synonyms: ['B', 'Berlin']
},
{
name: 'C',
synonyms: ['C', 'Paris']
}
]
}
}];
If you then have an Intent with a parameter of type Answer with the name answer, then you can test if intent.parameter.answer.resolved contains the expected letter.
Adding a visual interface
Using runtime type overrides are particularly useful if you also decide to add support for a visual selection response such as a list. The visual response builds on the runtime type override to add visual aliases that users can select on appropriate devices. When you get the reply, however, it is treated as if they said the entry name.

yaml safe_load of many different objects

I have a huge YAML file with tag definitions like in this snippet
- !!python/object:manufacturer.Manufacturer
name: aaaa
address: !!python/object:address.BusinessAddress {street: bbbb, number: 123, city: cccc}
And I needed to load this, first to make sure that the file is correct YAML, second to extract information at a certain tree-dept given a certain context. I had this all as nested dicts, lists and primitives that would be straightforward to do. But I cannot load the file as I don't have the original Python sources and class defines, so yaml.load() is out.
I have tried yaml.safe_load() but that throws and exception.
The BaseLoader loads the file, so it is correct. But that jumbles all primitive information (number, datetime) together as strings.
Then I found How to deserialize an object with PyYAML using safe_load?, since the file has over 100 different tags defined, the solutions presented there is impractical.
Do I have to use some other tools to strip the !!tag definitions (there is at least one occasion where !! occurs inside a normal string), so I can use safe_load. Is there simpler way to do solve this that I am not aware of?
If not I will have to do some string parsing to get the types back, but I thought I ask here first.
There is no need to go the cumbersome route of adding any of the classes if you want to use the safe_loader() on such a file.
You should have gotten an ConstructorError thrown in SafeConstructor.construct_undefined() in constructor.py. That method gets registered for the fall through case 'None' in the constructor.py file.
If you combine that info with the fact that all such tagged "classes" are mappings (and not lists or scalars), you can just copy the code for the mappings in a new function and register that as the fall-through case.
import yaml
from yaml.constructor import SafeConstructor
def my_construct_undefined(self, node):
data = {}
yield data
value = self.construct_mapping(node)
data.update(value)
SafeConstructor.add_constructor(
None, my_construct_undefined)
yaml_str = """\
- !!python/object:manufacturer.Manufacturer
name: aaaa
address: !!python/object:address.BusinessAddress {street: bbbb, number: 123, city: cccc}
"""
data = yaml.safe_load(yaml_str)
print(data)
should get you:
[{'name': 'aaaa', 'address': {'city': 'cccc', 'street': 'bbbb', 'number': 123}}]
without an exception thrown, and with "number" as integer not as string.

Meteor update Mongo Doc by ID

I'm trying to update a Mongo record via Chrome's console.
Posts.update('hexidhere', {$set: {title: 'something text here'}});
The problem is with docs that were created a Mongo terminal. They were assigned an id like so (_str and _proto are nested inside the _id):
_id: LocalCollection._ObjectID
_str: '54ff06801ad15adbb3d1090'
_proto: LocalCollection._ObjectId
title: 'dummy title here'
When I added another test doc via chrome's console (not a mongo terminal) It seems to have added an ID correctly, and everything works as expected:
_id: 'EtPt9ntXtxG4qo9Tb'
title: 'dummy title here'
My question is:
Does anyone know a way to make the ID always be simple HexStrings (like in the second example), or is there a correct method for accessing the nested str value in the LocalCollection (I've tried Mongo.ObjectID('hexidhere'), 'theidhere', and a whole bunch of other stuff)?
Mongo likes to use ObjectId for _id, Meteor opted to use String. To learn more see the now deprecated google groups convo: https://groups.google.com/forum/#!topic/meteor-talk/f-ljBdZOwPk
To get the string of an ObjectId use the str method as in ObjectId("310458asdf323452").str See here for more info: http://docs.mongodb.org/manual/reference/object-id/

Autocomplete function not working in meteor

I have an autocomplete textbox function which I am using in meteor.
It works fine for the following hardcoded data like :
$(document).ready(function() {
$("#demo-input-facebook-theme").tokenInput(
[{id: 7, name: "Ruby"},{id: 11, name: "Python"},{id: 13, name: "JavaScript"}],
{theme: "facebook"}
);
});
Now , I had fetched data from database MongoDB, when I pass this data as parameter to the autocomplete function it does not work... I have also used the method JSON.stringify().
The returned data looks like :
[{"_id":"ab170916-a44b-49f9-85ef-a34c90fb815d","Namelist_name":"Badminton"},
{"_id":"f768e4ba-b628-4d3f-8da6-0bad31346dcc","Namelist_name":"Biking"},
{"_id":"0bee086b-1785-40c9-9c5d-a39331c875e1","Namelist_name":"Chess"},
{"_id":"4eae1e54-ec60-4578-8052-0bf1bccf13b1","Namelist_name":"Golf"},
{"_id":"a0d2b89e-a2d6-4b30-8e38-779c5a886d49","Namelist_name":"Hiking"},
{"_id":"f3a05456-38d4-40f2-86b1-eddea061fdf0","Namelist_name":"Tennis"},
{"_id":"3669b9a2-3f87-4579-8064-82d627196fcb","Namelist_name":"Walking"},
{"_id":"6ac6497e-82b2-40fe-8b24-152e9f42750d","Namelist_name":"Wine Tasting"},
{"_id":"15a7ca87-aef7-43ab-945b-168647bb59aa","Namelist_name":"Yoga"},
{"_id":"bc40d166-64ef-4e61-85cd-60064dc037cd","Namelist_name":"Zumba"}]
Just change the Namelist_name with only name keyword. Since jquery tokeninput uses name as key
as mentioned in your hardcoded data. And if we download jquery tokeninput from http://loopj.com/jquery-tokeninput/. we come to know that the keyword is 'name'.
Hope this helps....
If you compare your returned JSON data with the test data that works there is one essential difference: you are missing the id field in your MongoDB JSON results and instead providing _id. The id field is currently a hardcoded default for the jQuery tokenInput plugin you are using for autocomplete.
Several folks have submitted patches to allow setting a different key using the tokenValue parameter.
Example (untested) patch: tokenValue cannot be changed.
If you're autocompleting multiple items with free text, you may want to check out this package I created:
https://github.com/mizzao/meteor-autocomplete

Defining a Mongoose Schema on the fly

I have a model file that gathers together all my Mongoose models. One of the models I would like to initialize with a variable number of fields. Currently I'm defining more fields than I think I am going to require:
TallySchema = new mongoose.Schema
0: Number
1: Number
...
20: Number
Obviously this is not ideal. I see Mongoose will let you specify options outside the Schema definition but can't see how to add new fields (or paths, I guess, in Mongoose).
Based on the mongoose plugin documentation it looks like you can just do:
schema.add({ field: Number })
This would need to be verified, but looking at the source it should be possible:
In the Schema constructor, it simply passes the definition object to this.add() (source).
The actual paths then get created within Schema.prototype.add (source).
So, seems like all you would need to do is something like:
// not sure what this looks like in CoffeeScript
TallySchema.add({ /* new property definition here */ });
I found this in the Mongoose documentation page:
var ToySchema = new Schema;
ToySchema.add({ name: 'string', color: 'string', price: 'number' });
You can use the 'mixed' type to encapsulate your values. It wouldn't be possible to have them be at the top level though but it works great otherwise.
new mongoose.Schema({
average: Number,
countPerRating: mongoose.Schema.Types.Mixed,
});
This is an excerpt from a mapreduce schema. I use the mixed type to store the number of times someone gives a certain rating, so we can say things like "10 1 star ratings, 45 4 star ratings", etc.
mixed type worked great for that.