本文介绍了用户#显示中的ruby on rails TypeError-无法像访问一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表单:

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

发布模型

belongs_to :user
has_many :likes

用户模型

  has_many :posts
  has_many :likes

类似模型

  belongs_to :post
  belongs_to :user

我不断收到以下错误:

TypeError in Users#show 
Cannot visit Like

Like.find(post.user.likes),

第一个解决方案:

改变

Like.find(post.user.likes),

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

第二个解决方案:

改变

Like.find(post.user.likes),

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

推荐答案

Like.find(post.user.likes)试图查找给定likes数组的Like.那真的没有多大意义.

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

您很有可能想搜索post.user.likes,但是即使那样,我还是有些困惑-您需要发布更多代码.

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正在寻找id(整数)或包含键和值的哈希.数组不提供任何搜索信息.

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.

编辑:如果您正在寻找当前用户的喜欢.您需要执行以下操作...

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

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

这篇关于用户#显示中的ruby on rails TypeError-无法像访问一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:16