ruby on rails TypeError in Users#show - Cannot visit Like

旧巷老猫 提交于 2019-12-11 02:24:53

问题


form that causes the problem:

<%= form_for Like.find(post.user.likes), :html => { :method => :delete , :class => "unlike_post_like_form" } do |f| %>

Post model

belongs_to :user
has_many :likes

User model

  has_many :posts
  has_many :likes

Like model

  belongs_to :post
  belongs_to :user

i keep getting the following error:

TypeError in Users#show 
Cannot visit Like

on

Like.find(post.user.likes),

EDIT:

First solution:

changing

Like.find(post.user.likes),

to

Like.find(post.user.likes).limit(1),

Second solution:

changing

Like.find(post.user.likes),

to

current_user.likes.where(:post_id => post.post_id)

回答1:


Like.find(post.user.likes) is attempting to find a Like given an array of likes. That doesn't really make much sense.

Most likely, you'll want to search post.user.likes, but even then, I'm a bit confused - you'll need to post more code.

Like.find is looking for either an id (integer), or a hash containing keys and values. An array doesn't give it any information to search.

EDIT: If you're looking for the likes of current user. You'll need to do the following...

current_user.likes.where(:post => @post)




回答2:


The type error comes from passing a collection (post.user.likes - which is an array) into a method that's expecting a record id, for example, Likes.find(3) or Likes.find(params[:id])



来源:https://stackoverflow.com/questions/12333466/ruby-on-rails-typeerror-in-usersshow-cannot-visit-like

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!