Ruby 欢迎光临#秀
以下是我得到的错误: 背景: 我试图根据一个动作是否完成来显示一个按钮。这是我的密码Ruby 欢迎光临#秀,ruby,ruby-on-rails-5,nomethoderror,Ruby,Ruby On Rails 5,Nomethoderror,以下是我得到的错误: 背景: 我试图根据一个动作是否完成来显示一个按钮。这是我的密码 <% if @courses_user.complete! %> <%= link_to "Completed", course, class: "block text-lg w-full text-center text-white px-4 py-2 bg-green hover:bg-green-dark border-2 border-green-dark leading-non
<% if @courses_user.complete! %>
<%= link_to "Completed", course, class: "block text-lg w-full text-center text-white px-4 py-2 bg-green hover:bg-green-dark border-2 border-green-dark leading-none no-underline" %>
<% else %>
<%= link_to "View Modules", course, class: "block text-lg w-full text-center text-grey-dark hover:text-darker px-4 py-2 border-2 border-grey leading-none no-underline hover:border-2 hover:border-grey-dark" %>
<% end %>
在我的用户模型中,我有
class CoursesUser < ApplicationRecord
belongs_to :course
belongs_to :user
has_many :course_modules_users
def completed!
self.update_attributes(complete: true)
end
end
课程用户
在Welcome,我有
class WelcomeController < ApplicationController
def show
@courses = Course.all.order(created_at: :asc)
@courses_user = CoursesUser.all
end
end
class WelcomeController
但是我得到了一个命名者,非常感谢您的帮助。您已经定义了
@courses\u user=CoursesUser.all
,因此它是一个集合而不是单个对象。而你不能调用完成集合上的代码>以及错误
解决方案:
反复浏览@courses\u user
并调用完成每个实例上的代码>如下所示
<% @courses_user.each do |cu| %>
<% if cu.complete! %>
<%= link_to "Completed", course, class: "block text-lg w-full text-center text-white px-4 py-2 bg-green hover:bg-green-dark border-2 border-green-dark leading-none no-underline" %>
<% else %>
<%= link_to "View Modules", course, class: "block text-lg w-full text-center text-grey-dark hover:text-darker px-4 py-2 border-2 border-grey leading-none no-underline hover:border-2 hover:border-grey-dark" %>
<% end %>
<% end %>
哦,好的,我的错误我正在更新属性,我只想检查它是否完整,更新我可以在其他地方使用的属性,但是对于这个页面,我只想检查课程是否标记为完整。是的,但是在模型中它有self.update\u属性
我可以在其他地方使用,但我想检查它是否被标记为真的,因为它标记所有课程都是完整的now@B.J.B如果要检查课程是否已完成,请尝试
<% @courses_user.each do |cu| %>
<% if cu.completed! %>
<%= link_to "Completed", course, class: "block text-lg w-full text-center text-white px-4 py-2 bg-green hover:bg-green-dark border-2 border-green-dark leading-none no-underline" %>
<% else %>
<%= link_to "View Modules", course, class: "block text-lg w-full text-center text-grey-dark hover:text-darker px-4 py-2 border-2 border-grey leading-none no-underline hover:border-2 hover:border-grey-dark" %>
<% end %>
<% end %>