Martin Ferretti

Joined

100 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in Global Autocomplete Search Discussion

i'm using this for "episodes" instead of "movies" so i have:

json.episodes do
json.array!(@episodes) do |episode|
json.title "Ep #{episode.episode}: #{episode.title}"
json.icon "#{episode.podcast.logo}"
json.url episode_path(episode)
end
end

json.podcasts do
json.array!(@podcasts) do |podcast|
json.title "#{podcast.title}"
json.icon "#{podcast.logo}"
json.url podcast_path(podcast)
end
end

and my search.js adds template function as well:

window.addEventListener("load", function() {
$input = $("[data-behavior='autocomplete']")

var options = {
getValue: "title",
url: function(phrase) {
return "/search.json?q=" + phrase;
},
categories: [
{
listLocation: "episodes",
header: "Episodes",
},
{
listLocation: "podcasts",
header: "Podcasts",
}
],
template: {
type: "iconLeft",
fields: {
iconSrc: "icon"
}
},
theme: "blue-light",
list: {
onChooseEvent: function() {
var url = $input.getSelectedItemData().url
$input.val("")
window.location = (url)
},
showAnimation: {
type: "fade",
time: 300,
callback: function() {}
},
hideAnimation: {
type: "fade",
time: 300,
callback: function() {}
},
}
}

$input.easyAutocomplete(options)
});

Posted in Global Autocomplete Search Discussion

if you're not using turbolinks (which i'm not), replace search.js with:

window.addEventListener("load", function() {
$input = $("[data-behavior='autocomplete']")

var options = {
getValue: "title",
url: function(phrase) {
return "/search.json?q=" + phrase;
},
categories: [
{
listLocation: "movies",
header: "Movies",
},
{
listLocation: "directors",
header: "Directors",
}
],
list: {
onChooseEvent: function() {
var url = $input.getSelectedItemData().url
$input.val("")
window.location = (url)
}
}
}

$input.easyAutocomplete(options)
});

Posted in Liking Posts Discussion

Nevermind, problem solved. Instead of doing order(created_at: :desc), i did order("likes.created_at desc") and now they are sorted by the like date instead of the post date. Thanks!

Posted in Liking Posts Discussion

That's what I'm doing...I have current_user.liked_posts.order(created_at: :desc) and it shows the correct posts liked by the user BUT they are ordered by the created_at date of the posts, not the likes.

Posted in Liking Posts Discussion

I've been playing with joins but can't seem to find the answer: I can displayed a user's liked posts (current.user.liked_posts.each do) but they are sorted by the creation date of the post. How can I sort by the like.created_at date?

Posted in Comments With Polymorphic Associations Discussion

How would this be handled if we only want the user to see their own comments, but not anyone else's?

Posted in Liking Posts Discussion

That's what I did but it works when the code is inline in the view itself not when referenced as a partial.

Posted in Liking Posts Discussion

A follow-up question...the "like" button works in my show action as that does use @book, but when going through the for each statement above, it will not work as the variable is no longer @book but book. Is there a workaround to this?

Posted in Liking Posts Discussion

Just what I needed, thanks Chris! I did go through the has_many documentation for rails and had to change class_name: "Book" to to source: :books though to avoid the to_sym error I was getting. Now I just need to figure out how to make the buttons load via ajax, that only works in the show action. Keep up the awesome work!

Posted in Liking Posts Discussion

Been looking for a way to do this for a while, great episode...now how would I list all the all the posts that a user has liked? For example, I can do something like this:
<ul>
<%= current_user.owns(@book).each do |book| %>
<li><%= book.id.to_s %></li>
<% end %>
</ul>

...but alas this only shows me the own.id whereas I would like it to show me, for example the book.title. Also, this works with the show action, but in my index it doesn't as I have set up a <% @book.each do |title| %>. I can place the code inline, replacing @book with title but then my ajax button change is rendered useless. I'm fairly new to Rails so any ideas would be welcome!