Creating tags on posts RAILS - ruby-on-rails-3

I am making something that's like that autocomplete tags field for the posts on stackoverflow.com
I want to make it so that when you make a blog post, you can tag it with words in a database, similar to SO.
For the posts, it belongs_to_and_has_many tags
For the tags, it belongs_to_and_has_many posts
However it is a problem for me to do it on the same page because #post would be nil.
How can I implement this?
(If someone can give me the code for the stackoverflow ask question page that would be AWESOME)

I think the problem here is that it seems like you are asking two different questions. Please correct me if I'm wrong on this.
The first question is how you would implement the autocomplete feature to allow users to easily select from a pre-populated list of tags. To answer this, you might refer to spncrgr's answer above.
The second question is how to deal with associating these retrieved tags to the current post. For this you can add additional javascript functionality to your autocomplete solution. When a user selects which tag they want from the autocomplete field, you can do like StackOverflow does and add the tag to a list of tags in a single text field. These can be either space or comma delimited. When you submit the form to create the new post, you can parse this field into it's separate tags:
tags = params[:tags].split(' ')
You can then associate these tags to the model in the Post#create action.

This may not help you at all (or you may have already seen it), but here's a link to a Railscasts' episode on auto-complete:
http://railscasts.com/episodes/102-auto-complete-association
I know it helped me when trying to do something similar.
HTH

It looks for me like you want to generate tags automatically.
You could create
class Post
before_save :create_tags
private
def create_tags
# get your tags somehow
self.tags << Tag.new(:text=>"...")
end
end
method in models/post.rb and build them there.

If you want to search among existing tags for your auto-suggest, you should have it match from Tag.all, as that won't be nil, just as you would collect a group of objects in a select drop down. Not sure about the code for auto-complete, but the tags should be in the Tag table.

Related

Using the Reddit API, is it possible to return a list of comments if the submission title includes a specific keyword?

Using the Reddit API, is it possible to return a list of Reddit comments if the submission title includes a specific keyword? For example, if the keyword is "Lime Sparkling Water", I want to return all the comments under submissions that have "Lime Sparkline Water" in the title.
I've tried using the Pushshift API for Reddit but looks like we can only isolate the submission data or the comment data and not isolate the comments data based on the submissions data.
Please help :)
Yes, this is possible with PRAW.
You can use PRAW's stream function, that page also has examples how to use PRAW.
An example being:
subreddit = reddit.subreddit("AskReddit")
for submission in subreddit.stream.submissions():
# do something with submission
...
This will return all submissions within "AskReddit". From there you could check the post title:
if 'Lime Sparking Water' in submission:
# do something with the submission
Although, I know this is a hypothetical phrase to search, you'd be better off searching lowercase phrases/words and .lower()

Show content based on whether the user has certain tag in Mailchimp

I went through the Merge Tags here and here, but couldn't figure out the syntax that would allow me to show content based on whether the user has certain Tag or not.
Help?
My goal in case it helps:
User subscribes, and is queued for a welcome mail one day later. In meantime that user may get tagged (my way of segmenting them), and so, the next day when that user receives the welcome mail, the content needs to be catered based on the tag that user got.
Got a response from their support saying
merge tags do not work with Tags just yet
here's the whole thing:
While we do have conditional merge tags available, I'm afraid we do
not have any that would work with Tags. To be transparent, Tags were
recently added a few months ago, and there are some features in our
application that has not updated to work with Tags just yet.
Because conditional merge tags do not work with tags yet, the best
option would be to create multiple automations and send them out based
on each tags. If you do it that way, you'll be able to target those in
specific tags with specific content
Dug a little deeper from the first link. There is another link Use Conditional Merge Tag Blocks which contained the below code:
Name
IF-ELSE
Definition
Use ELSE to indicate alternative content to display if the *|MERGE|* tag value is false.
Example
*|IF:MERGE|* content to display *|ELSE:|* alternative content to display *|END:IF|*
Name
ELSEIF
Definition
Use ELSEIF to specify a new *|MERGE|* tag to be matched against if the first *|MERGE|* tag value is false.
Example
*|IF:TRANSACTIONS >= 20|* Enjoy this 40% off coupon! *|COUPON40|*
*|ELSEIF:TRANSACTIONS >= 10|* Enjoy this 20% off coupon! *|COUPON20|*
*|ELSE:|* Enjoy this 10% off coupon! *|COUPON10|* *|END:IF|*
More examples with definitions can be found here.
Hope this is the answer you were after.

Shopify: Filtering collections by custom filter

I'm new with liquid and ruby, but I would like to create a custom filter in a collection, to filter by metafields. I already have:
A dropdown in the collection.liquid, with the values I would like to filter for.
When selecting a filter, it goes to a link like: https://myshop.myshopify.com/collections/my-collection/my-filter . Basically it is like the tags, but with my filter instead
However, since it is a custom filter and not a tag, I get no results. I'm wondering where is the query that displays all the products (or filters) is in the code. I know that it depends on the theme, but I'm using the default theme: launchpad-star.
Not sure if I could do it this way or with a link like: https://myshop.myshopify.com/collections/my-collection?filter_by=my-filter , in which case, I would also need where should the logic go.
I've looked at the forums already and found two closed tickets with no responses: https://ecommerce.shopify.com/c/ecommerce-design/t/using-metafields-to-create-filter-drop-downs-in-collection-liquid-187513 and https://ecommerce.shopify.com/c/ecommerce-design/t/using-metafields-to-create-filter-drop-downs-in-collection-liquid-134401 .
Thanks in advance
Probably not the best solution, but this is what I did to solve the problem:
I changed to the second option of the url, so when a user selects an option in the combobox, it is sent to a URL like: myshop.myshopify.com/collections/my-collection?filter_by=my-filter
In product-grid-item.liquid, I'm getting the metafield value of the product and displaying it as a class, and hide all the products as default. In the collection.liquid I read with javascript the value of the parameter (filter_by) and remove the "hide" class of the products with the value of the filter_by as class, so it gets displayed.
I feel that it is not very clean, but it is working as expected. Problems with this solution:
* Not displaying all the products and then filtering them
* I need to display all the products to avoid pagination, which could be a big problem if I have a lot of products.
If anyone could post a better solution, welcome!.

Create custom advance search filter in CGridView in YII

I have a model which contains emails with some other fields.
I want a custom filter in Yii CGridView's advance search which when applied, lists only Invalid Email IDs (using regular expression '^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$')
Note: I dont want to add any column in CGridView
I hope to help you out,
you will need several things
the line for filtering in the column of your choice...(i recommend you create a new attribute for this, ask me in comments if you want to know more why.) this goes in the cgridview of course:
'filter'=>CHtml::activeCheckBox($model, $attributeEmail)
the condition in your search of function that brings up the model.
Supposing you have a criteria inside your search in your model wich help you with your filtering what you need is ...
if($this->EMAIL == TRUE)
{
$criteria->addCondition("\"t\".\"EMAIL\" email NOT LIKE '%_#__%.__%'");
}
Why not to use regex and make a KISS approach ? better read this first...
Sql script to find invalid email addresses
I'd be glad to hear your comments it is interesting question for yii dev's btw

Rails 3 - which sql query for related tags

I'm planning this feature now for a long time, and I just can't get started with it really, cause I don't know how to express it in code. Sometimes when I think I got it and know what I want, I suddenly get trapped again and everything stops making sense.
I have tags and taggings, so a has_many through relation with articles. You could call article.tags and tag.articles.
Now, each tag has their show page, basically like stackoverflow. And on this show site I want to list related tags, among others. My approach to these related tags is, that it should be those tags, which most often are tagged as well, at an article, which is tagged with the show tag. I hope this makes some sense.
Example: I'm on /tags/obama, so the related tags should be those that most often are used at articles, that include the tag obama. If I had 4 articles, and 3 of them included 'obama' and all of those 3 as well included the tag 'united_states' for example, then the most related tag to tag 'obama' would be 'united_states'. Sorry if I'm wordy..
I'm not even sure, if this is the best approach to find related tags, but this idea works fine for me. However, I can't implement it.
First I would need to fetch all articles, that include the show tag. So tag.articles. But what's the next step?
tag.articles.each do |article|
article.tags
... im just getting confused at this point.
I think the best way to solve this is to have a many to many relation between tags, so a tag can have many tags. Then in the relation between two tags, you store the count of how many instances they occur together.
You could also simply create a new tag-to-tag connection each time the tags occur in the same article. This will however create some redundancy in the database.
If you do not want to introduce another table, you can get this to work the way you started, except it might be very slow with even a fairly small amount of tags. But here is how I would have done this, if you can not make a Tag-to-tag connection:
hash_storage = Hash.new(0) #0 is the default value
tag.articles.each do |article|
if article.tags.each do |t|
#we now know that this tag "t" is in the same article as our original tag
if t!=tag #we don't care if t actually the same as our original tag
hash_storage[t]+=1
end
end
end
#Now, this is a bit messy, but we need to sort the hash.
ordered_tags = hash_storage.map{|k,v| [v,k]}.sort.reverse.map{|a,b| b} #there might be a smarter way of doing this.
ordered_tags.each do |t|
#do whatever. the tags should now be ordered by their relative frequence of occurrance together with the initial tag.
end
Hope this helps :)