Liking Posts Discussion
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?
Ou I found the problem out :)
def set_profile
@profile = Profile.friendly.find(params[:profile_id])
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!
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?
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?
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.
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.
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.
Hi Chris,
Why did you decide to roll your own liking system rather than using `acts_as_votable`?
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.
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.
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?
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.
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!
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
solved it by using
<%= link_to 'Like', listing_like_index_path(listing.mls_id), method: :post %>
dumb mistake on my part
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.
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.
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.
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?
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