Rails f.select 试图禁用下拉列表的更改 [英] Rails f.select trying to disable a dropdown from being changed

查看:56
本文介绍了Rails f.select 试图禁用下拉列表的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试禁止在表单上更新下拉菜单.

I'm trying to disable a dropdown from being updated on a form.

目前我的表单中有这一行:

Currently i have this line in my form:

<%= f.select :permission, options_for_select([['Admin', 'admin'], ['Read Only', 'readonly'], ['Editable', 'editable']], {:disabled => @permissions_disabled}) %>

我的编辑控制器方法包含:

With my edit controller method containing:

@permissions_disabled = params[:id].to_i == current_user.id.to_i
p @permissions_disabled

我可以在我的日志中清楚地看到 1@permissions_disabled1 是真的,但是当我编辑表单时,我仍然可以在下拉列表中选择新值.

I can clearly see in my log that 1@permissions_disabled1 is true, but when i edit the form, i can still select new values in the dropdown.

我在这里做错了什么?

推荐答案

select 接受 5 个参数,第 4 个是 helper 的一组选项.第 5 个是 html 选项,如 class 和 id.我认为你需要把它传递给那个

select accepts 5 parameters, the 4th one is a set of options for the helper. the 5th one is the html options like class and id. I think you need to pass it to that

<%= f.select :permission, options_for_select([['Admin', 'admin'], ['Read Only', 'readonly'], ['Editable', 'editable']], {}, {:disabled => @permissions_disabled}) %>

更新:在您的代码中没有看到 options_for_select.如果您使用 select,则不需要它,您只想在使用 select_tag

UPDATE: didn't see the options_for_select in your code. you don't need that if you're using select, you'd only want to use that when you're using select_tag

<%= f.select :permission, [['admin', 'Admin'], ['readonly', 'Read Only'], ['editable', 'Editable']], {}, {:disabled => @permissions_disabled} %>

这篇关于Rails f.select 试图禁用下拉列表的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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