[英]undefined method `some_method' for “some_variable”:String. Ruby on Rails
I'm trying to I'm trying to show table, but I get an error undefined method `name' for "Sphere Kharkov":String This is my Instance Variables: 我正在尝试显示表,但出现“ Sphere Kharkov”错误未定义的方法“名称”:字符串这是我的实例变量:
@result1 {"No department"=>"1", "Dep2"=>"1", "Sphere Kharkov"=>"2"} @ result1 {“无部门” =>“ 1”,“ Dep2” =>“ 1”,“球形哈尔科夫” =>“ 2”}
@result2 {"Sphere Kharkov"=>"1"} @ result2 {“ Sphere Kharkov” =>“ 1”}
@department_names ["Sphere Kharkov", "Dep2", "Dep without members", "No department"] @department_names [“ Sphere Kharkov”,“ Dep2”,“没有成员的Dep”,“无部门”]
Controller's methods 管制员的方法
def age_profile
@result1 = age_report(1,9)
@result2 = age_report(10, 12)
@department_names = current_company.departments.map(&:name)
@department_names << "No department"
end
def age_report(start_age, end_age)
result = {}
User.select("COALESCE(departments.name, 'No department') AS name, COALESCE(count( * ), '0') AS count")
.joins("LEFT JOIN departments ON departments.id = users.department_id")
.where(:users => { :is_deleted => 0, :company_id => current_company.id})
.where("TIMESTAMPDIFF(YEAR, `date_of_birth`, CURDATE()) BETWEEN :start_age AND :end_age", start_age: start_age, end_age: end_age)
.group("departments.name")
.each {|d| result[d.name] = d.count }
result
end
In View: 在视图中:
%table.table.table-striped.table-bordered
%tr
%th= t('Depatment')
%th= t('1-18')
%th= t('18-25')
%th= t('25-45')
-@department_names.each do |f|
%tr
%td= f.name
%td= @result1[f.name]
%td= @result2[f.name]
Сould you help me please. 请你帮我一下。 What's wrong?
怎么了?
This line: 这行:
@department_names = current_company.departments.map(&:name)
Retrieves only the names of the companies. 仅检索公司名称。 So basically you are calling the name method on the result of the name method.
因此,基本上,您是在name方法的结果上调用name方法。
So either remove .map(&:name)
or just use f
insteaf of f.name
. 因此,请删除
.map(&:name)
或只使用f
insteaf的f.name
。
@department_names
variable contain strings that don't respond to name
method. @department_names
变量包含不响应name
方法的字符串。 Simple solution is: 简单的解决方案是:
- @department_names.each do |f|
%tr
%td= f
%td= @result1[f]
%td= @result2[f]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.