我已经确认我的很多关系正在通过终端工作,因为我创建了一个关系,它显示在我的数据库中。我的问题集中在专门针对视图和控制器本身进行更改,而我还没有找到许多具体的答案。例如在pit index.html.erb中我收到此错误
undefined method `users' for #<Pit::ActiveRecord_Relation:0x00000106230df0>
下面的这个标记位
<p>Pit Created by: <%= link_to @pit.users, pit.user %> on <%= pit.created_at.strftime("%d %b. %Y") %></p>
我目前在我的维修控制器中有这个 -
def index
if params[:tag]
@pit = Pit.tagged_with(params[:tag])
@user = User.find_by(params[:id])
@pits = Pit.paginate(page: params[:page])
else
@pit = Pit.all
@user = User.find_by(params[:id])
@pits = Pit.paginate(page: params[:page])
end
end
我的模型由
组成用户
class User < ActiveRecord::Base
acts_as_voter
has_many :joinables
has_many :pits, :through => :joinables
has_many :comments
enum role: [:user, :vip, :admin]
after_initialize :set_default_role, :if => :new_record?
坑
class Pit < ActiveRecord::Base
validates :topic, :author, :summary, presence: true
acts_as_taggable
acts_as_votable
has_many :comments
has_many :joinables
has_many :users, :through => :joinables
mount_uploader :image, ImageUploader
class Joinable < ActiveRecord::Base
belongs_to :pit
belongs_to :user
end
我知道这可能很容易,但我无法弄清楚究竟要改变什么而不会破坏任何东西。我需要在我的def索引中添加另一个实例变量吗?我知道我很接近,我只需要一点方向。谢谢。
答案 0 :(得分:0)
当您收到错误消息时,它有助于删除您的应用的任何特定内容。例如,谷歌搜索&#34;未定义的activerecord :: relation&#34;出现了this post,这应该回答你的问题。
答案 1 :(得分:0)
由于tagged_with
会返回许多记录,因此您需要在{tagged_with()'调用中添加.first
:
@pit = Pit.tagged_with(params[:tag]).first
...或迭代维修记录,如果这是你的意图。
BTW - 由于find_by
只返回一条记录,这就是为什么你不需要.first
,以防你感到困惑。