Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails li类中的数据属性?_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 3.2_Haml - Fatal编程技术网

Ruby on rails li类中的数据属性?

Ruby on rails li类中的数据属性?,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.2,haml,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.2,Haml,我的模型中有以下代码: def features_to_html_class "mix #{bedrooms} #{region.name} #{categories.map{|cat|cat.name}.join(' ')}" end 在我看来 - @regions.each do |region| - @houses.where(region_id: region.id).each do |house| %li{:class => house.featur

我的模型中有以下代码:

def features_to_html_class  
   "mix #{bedrooms} #{region.name}  #{categories.map{|cat|cat.name}.join(' ')}"
end
在我看来

- @regions.each do |region|
   - @houses.where(region_id: region.id).each do |house|
     %li{:class => house.features_to_html_class }
HTML输出如下所示:

<li class='mix 3 umbria price_range-1 villa_with_pool '>
  • 更新

    由于数据值是动态的,因此可以更改方法以提供整个属性哈希:

    def features_to_html_attributes  
      {
        class: "mix #{bedrooms} #{region.name} #{categories.map{|cat|cat.name}.join(' ')}",
        data: {sort: 'data-name', order: 'desc'}
      }
    end
    
    并通过以下方式分配:

    %li{features_to_html_attributes}
    

    要在Haml中添加新属性,只需将它们作为散列传递('attribute_name'=>'value'))

    下面应该做什么

    - @regions.each do |region|
       - @houses.where(region_id: region.id).each do |house|
         %li{:class => house.features_to_html_class, "data-sort" => "data-name", "data-order" => "desc"}
    
    编辑:

    为什么不能在视图中使用方法“features\u to\u html\u class”的更改,因为 您已经直接编写了it类属性(附加在li元素类中的值)


    %li{:class=>house.features\u to\u html\u class}
    试试
    %li{:class=>house.features\u to\u html\u class,数据排序:“数据名”,数据顺序:“desc”}
    是这些值(
    'data-name'
    'desc'
    )修复了还是动态获取?谢谢…值是动态的,因此您必须为此创建单独的方法