【发布时间】:2023-04-28 01:57:01
【问题描述】:
当我运行这样的操作时,我的代码可以工作,
def interest
@interest = Interest.new
@interest.user_id = current_user.id
@interest.idea_id = @idea.id
@interest.save
@ideas = Idea.take(10)
flash[:notice] = "A message has been sent to the poster."
render "ideas/forum"
end
但是,当我在兴趣操作中使用此行时,为什么会得到一个未定义的“兴趣”方法?
@interest = current_user.ideas.interest.create(params[:interest])
这是我的创意模型
class Idea < ActiveRecord::Base
belongs_to :user
:title
:category
:content
:user_id
:createdDate
:updatedDate
这是我的用户模型(设计)
class User < ActiveRecord::Base
has_many :ideas
has_many :interests
end
这是button_to标签
<%= button_to "I'm Interested", ideas_interest_path(:id => idea.id, :idea_id => idea.id, :user_id => idea.user_id) ,class: 'btn btn-primary' %>
还有我的路线,
resources :ideas do
resources :interests
end
兴趣模型 类兴趣
has_many :users
:idea_id :user_id
结束
NoMethodError - undefined method `interest' for #<Idea::ActiveRecord_Associations_CollectionProxy:0x007f9e5189bab0>:
活动记录(4.2.0)
【问题讨论】:
-
也添加您的兴趣模型和错误。
-
在您的 Idea 模型中添加
has_and_belongs_to_many :interests。 -
您的兴趣模型在哪里?你在哪里添加的?
-
@SharvyAhmed 从想法和兴趣尝试,关系没有改变错误。
-
@SharvyAhmed 再次检查,我认为是编辑,之前删除了它。
标签: ruby-on-rails