Ask A Question

Notifications

You’re not receiving notifications from this thread.

Liking Posts Discussion

Discussion for Liking Posts

Hi, Chris. I am using the friendly_id gem to make the profiles url.
On my like controller I have this def set_profile @profile = Profile.friendly.find(params[:id]) end.
Everything work great but when I click like button I get 404 erro message to console.
The link looks like this
Like and the console error is
POST jquery.self-a714331225d...... http://localhost:3000/profiles/vjandrei/like 404 (Not Found) What I miss here?

Reply

Ou I found the problem out :)

def set_profile
@profile = Profile.friendly.find(params[:profile_id])

Reply

I am also having the 404 issue but it's something to do with routes i'm guessing?

ActionController::RoutingError (uninitialized constant Developments):

and then in the console:

POST http://localhost:3000/developments/1/like [HTTP/1.1 404 Not Found 1343ms]

When i run rake routes I get the following:

new_development_like GET /developments/:development_id/like/new(.:format) developments/likes#new

edit_development_like GET /developments/:development_id/like/edit(.:format) developments/likes#edit

development_like GET /developments/:development_id/like(.:format) developments/likes#show

PATCH /developments/:development_id/like(.:format) developments/likes#update
PUT /developments/:development_id/like(.:format) developments/likes#update
DELETE /developments/:development_id/like(.:format) developments/likes#destroy
POST /developments/:development_id/like(.:format) developments/likes#create

Any feedback is appreciated. Thanks!

Reply
Josh Zandman Josh Zandman

Thanks Chris, this episode is so helpful!

Reply

Hi Chris, I implemented this liking posts and it was working fine until I added Friendly_ids to the urls.
When adding the friendly_ids , the show action in Posts controller was changed from @post = Post.find(params[:id]) to @post = Post.find_by slug: params[:id]. I have tried replacing post.id under the likes_create/destroy to post.slug but it doesn't work.
Can you please advice on how to fix this?

Reply

Hi Chris,

I've got this working well without ajax, but as soon as I add 'remote: true' I get a 404 error in the console. The button is pressed, but doesn't update.

Any ideas as to why this is?

Reply
Lee Humphreys Lee Humphreys

Hi Chris I applied this to a project I'm working on and managed to get it working as per the video, so many thanks. It helped me a great deal, still a newbie.
How would you go about creating a separate view to show all the objects you have liked.

Reply
Lee Humphreys Lee Humphreys

I have this working as per the tutorial but wanted to have a link in my navbar to show all the likes by the user, i.e. my likes, has anyone implemented this tutorial in this way.

Reply

All you have to do is add a controller for it and then you can loop through current_user.likes.each do |like| and print them out on the page.

Reply

Hi Chris,
Why did you decide to roll your own liking system rather than using `acts_as_votable`?

Reply

Main reason is it's such a simple feature, no need to use a gem. Always great to have one less dependency, less likely to break on upgrading Rails versions, etc. And the benefit is I can customize it and use it exactly how I want.

Reply
Jeremy Christopher Bray Jeremy Christopher Bray

Hi @excid3:disqus loving gorails. Would be super helpful for newbies if all the screenshots were in the transcript. Was chasing my tail with a stupid relationship problem, until I went to the source files.

Reply

Hi Chris, just subscribed, really nice stuff here. Regarding this tutorial, I have a question about caching: how would you implement this like feature without having to use `current_user` as the cache key (which obviously would defeat the purpose of the cache in the first place) ? Would you recommend using a gem like `render_async` to do this?

Reply

Well, you can cache the user signed out version and use that everywhere. That will cover a lot of things if your website gets much traffic from search results. The other thing I'd say is to use an AJAX request or just embed the IDs in the HTML somewhere for the logged in users and just have JS update the cache'd snippet visually to reflect whether the user had voted on it.

This way you can use the same cache for literally everyone and then just tweak it visually based upon the current user with a little JS. That's how I'm handling the watchlist and completed states of videos on GoRails now so I can have a single cache for every episode and then update it visually for logged in users.

Reply

Hi Chris, it's been a while since you posted this, but I'm having an issue and can't figure it out. Basically when I click "Like" everything seems to be working fine and the counter goes up and the button changes to "unlike". But when I click "Unlike" nothing happens until I go to another page. Any idea why that might be? Thanks in advance!

Reply

Great Video. However I'm stuck and would love some help.
I have listings results incoming from an API. On my index view I want to add the like or heart button however when i add

<%= link_to 'Like', listing_like_index_path(@listing), method: :post %> it's just break and it's because I don't have :listing_id

these are my routes for like
resources :listings, only: [:index, :show] do
resources :like, module: :listings
end

I added resources instead of resource because after the click the button I want them to stay on the index view

Any help will be deeply apreciated

Reply

solved it by using

<%= link_to 'Like', listing_like_index_path(listing.mls_id), method: :post %>
dumb mistake on my part

Reply

Hi Chris, I've been followed you along for a while, (I just changed the account frequently because I change the workplace as well) and I really appreciate your work. I just wonder that do you share the beginning project of each video. Because I've been feeling kind of hard to follow along your video without the beginning project. Hope you can consider this. Thank you so much. Keep up the good work.

Reply

The base project is generally using https://github.com/excid3/jumpstart

Reply

Thank you very much!

Reply

Hi Chris,

When I make the AJAX request to like a post I receive the following error in the console:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

The logs show the following error:

Rack app error handling request { POST /posts/7/like }

#<ActionController::UnknownHttpMethod: POST, accepted HTTP methods are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, ORDERPATCH, ACL, SEARCH, MKCALENDAR, and PATCH>

Back in the console the error links to the rails-ujs file and shows me the error at the following location:

if (xhr.readyState === XMLHttpRequest.OPENED) {
**  return xhr.send(options.data);
**}

I'm running on Rails 5.2 if that helps.

Reply

Hi Chris,

I want to ask you a question, I got stuck at the part of implementing the likes? method in the user controller. Except my method was named saved? as was my table and related files. I kept getting the error unitialized constant Item::Safe, for hours I tried to fix this and my code all seemed fine. I finally just renamed my table and related files to bookmarks instead of saves. After updating all the models, controllers, routes, ect. It worked... Do you have any insight on this? Is it possible to be a naming conflict?

Thanks in advance.

Reply

Yeah, if you ever overwrote the save method like with an association, then that would cause problems. Almost certainly was your issue.

Reply

"Bookmark" might be an easier name to work with on the backend and then just use a different term for the UI.

Reply

Appreciate the speedy response. Thanks again.

Reply

thanks to all that are participating in this forum, one thing though is how do i make the posts likeable every 24 hours. as in liking the post becomes valid after that time of 24 hrs?

Reply

Hi newbie question: Will this work in a Rails 6 project? Thanks

Reply

Would be great if the screencast could be updated to factor in changes in Rails

Reply

It works on Rails 6 after adding this to
config > webpack > environment.js

const { environment } = require('@rails/webpacker')

const webpack = require('webpack')
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jquery: 'jquery/src/jquery',

    jQuery: 'jquery/src/jquery',
    'window.jQuery': 'jquery'
  })
)

module.exports = environment

Reply

i think it will be more interesting if it can be done with Stimulus Reflex

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.