问题
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