【问题标题】:how to replace instance variable with local variable in HAML如何用HAML中的局部变量替换实例变量
【发布时间】:2017-07-11 09:17:22
【问题描述】:

我安装了 gem rails_best_practices,我想知道如何用局部变量替换我的实例变量?

我尝试过使用:locals => { :department_category => @department_category },但我不确定这是否是我的代码中甚至会给出错误的部分

问题行如下:

_category_edit.haml

%a.black-link.bold{href: admin_content_departments_category_edit_path(params[:department_id], params[:child_department_id], category.id), :class => ("active" if @department_category.id == category.id)}
  #{category.title}

为什么这段代码会报错?

【问题讨论】:

  • 错误是什么?
  • replace instance variable with local variable
  • 为什么这段代码会出错?你提到的错误是什么?
  • 这是我不确定的,但我似乎在我的所有部分中都得到了它们,我正在尝试重构所有内容以按照here找到的“最佳实践”指南进行@
  • 你做得对。在作为 局部变量 传递的视图中使用department_category

标签: ruby-on-rails haml


【解决方案1】:

用局部变量替换实例变量

我相信你正在用:locals => { :department_category => @department_category } 渲染_category_edit.haml,那么你应该使用department_category(局部变量) 而不是@department_category(实例变量)在_category_edit.haml

#_category_edit.haml
%a.black-link.bold{href: admin_content_departments_category_edit_path(params[:department_id], params[:child_department_id], category.id), :class => ("active" if department_category.id == category.id)}
  #{category.title}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多