我面临问题
undefined local variable or method `student_details'
同时尝试编写一个Ruby方法,以便在浏览器中显示动态数据。我使用了view_component gem。我的"组分.rb";文件如下所示:
module Shared
module Student
class Component < ViewComponent::Base
def initialize(title:)
@title = title
end
student_details = {name: "John Doe", course: "Rails Development", student_id: "1"}
end
end
end
并且";component.html.erb";看起来是:
<div class="font-montserrat flex flex-col bg-white text-shadow-none">
<%= student_details.student_id %>
<%= student_details.name %>
<%= student_details.course %>
</div>
只能通过实例变量(@var
(或ruby方法(def method
(访问数据。您可以使用变量来完成此操作
module Shared
module Student
class Component < ViewComponent::Base
def initialize(title:)
@title = title
@student_details = {name: "John Doe", course: "Rails Development", student_id: "1"}
end
end
end
end
或者用方法
module Shared
module Student
class Component < ViewComponent::Base
def initialize(title:)
@title = title
end
def student_details
{name: "John Doe", course: "Rails Development", student_id: "1"}
end
end
end
end