0
我目前有一个名为Problem的模型。这通过belongs_to和用户has_many问题关联到用户。用户可以对问题进行投票。有了这个我有投票模型与belongs_to /索引问题和用户。创建记录时通过多个关联ID
此代码创建我的投票,但它不会带出用户ID。最终我想让用户只能对问题进行一次投票。我怎样才能得到它,所以它创建时接受两个参数,而不仅仅是problem_id。
值得关注的问题/ Vote.rb
def upvote
@problem = Problem.find(params[:id])
@problem.votes.create
redirect_to(problem_path)
end
的routes.rb
resources :problems do
member do
post 'upvote'
end
index.html.erb
<%= button_to '+1', upvote_problem_path(problem), method: :post %>
完美的作品。我的确在使用设计,并且在投票表上已经有了ID。非常感谢Baldrick。 – DMH