Rails-选择中的禁用选项(基于条件) [英] Rails - disable option in select (based on the condition)

查看:77
本文介绍了Rails-选择中的禁用选项(基于条件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我选择了:

= f.select(:category_id, @categories, :html_options => {:class => 'select_box'}, {:disabled => true if category.id == 18})

上面的代码段显然返回错误,但是如何根据id禁用选项?

The piece of code above obviously returns an error, but how to disable an option according by id?

推荐答案

还没有测试过,但是在您的控制器中您不能这样做

Haven't tested this but in your controller could you not do

@checkvar = @category.id == 18 ? true : false

然后在视图中

f.select(:category_id, @categories, :html_options => {:class => 'select_box'}, {:disabled => @checkvar})

或在模型中编写要测试的功能

or in the model write a function to test

def disable_select 
    if self.id == 18
        true
    else 
       false
    end
end

然后在视图中

f.select(:category_id, @categories, :html_options => {:class => 'select_box'}, {:disabled => @category.disable_select})

这篇关于Rails-选择中的禁用选项(基于条件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆