0

I'm using the friendly_id gem for modifying my urls. But i have a problem with my likes system.

For the use of friendly_id i change my controller:

@software = Software.find(params[:id])

to

@software = Software.friendly.find(params[:id])

So my like.js do not recognize the id of my model. Here is the error:

ActionView::Template::Error (undefined method `id' for nil:NilClass):
    1: $('#like-icon-software-<%= @software.id.to_s %>').
    2:   html('<%= j render "softwares/like_icon", {is_liked: @is_liked, software: @software} %>');

Thank you for your help and sorry for my english ..

EDIT 1:

I get in the params :

Processing by LikesController#create as JS Parameters: {"software_id"=>"Odoo"} 

instead of

Processing by LikesController#create as JS Parameters: {"software_id"=>"1"} 

without friendly_id.

models/software.rb

extend FriendlyId
friendly_id :title, use: [:slugged, :finders]

def is_liked user
    Like.find_by(user_id: user.id, software_id: id)
end

How to continue to receive the id and not the "slug" ?

Clyde T
  • 141
  • 10
  • What do you get in params for this action? Do you have `id` inside params? You can check it in logs – Vasilisa Dec 18 '18 at 19:27
  • @Vasilisa Hi, i get in the params : Processing by LikesController#create as JS Parameters: {"software_id"=>"Odoo"} instead of {"software_id"=>"1"} without friendly_id. I edit my question with more info – Clyde T Dec 19 '18 at 00:15

2 Answers2

0

I think you are getting the 'title' of model 'software' in params software_id, as you have used it as a slug for friendly_id. Software.friendly.find(params[:id]) brings record for the matching friendly_id. You will get the id as follows,

@software = Software.friendly.find(params[:id])
@software.id
  • Thank you for your answer, but i do not understand how you want to implement your solution. Because i am already looking for the @software.id in the like.js – Clyde T Dec 19 '18 at 11:49
0

In your like.js could you please use this:

$('#like-icon-software-<%= software.id.to_s %>')

instead of this:

$('#like-icon-software-<%= @software.id.to_s %>')
mehedi
  • 466
  • 3
  • 8
  • Thank you for your reply. But that does not change the problem. My system of like works without friendly_id so the problem is that the slug is called in the params and not the id. – Clyde T Dec 19 '18 at 14:59