Ruby-on-rails 如何在 rails 3.1.0 视图中设置只读字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9120904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:45:04  来源:igfitidea点击:

How to set a field read only in rails 3.1.0 views?

ruby-on-railsruby-on-rails-3.1simple-form

提问by user938363

My question is how to set a field in rails form read only. The following is a selection box in quotes controller. Users are not allowed to change the selection.

我的问题是如何在 rails 表单中设置只读字段。以下是报价控制器中的选择框。不允许用户更改选择。

  <% @quote.test_items.each do |t| %> 
    <%= f.association :test_items, :label => false, :selected => t.id %>
  <% end %>

The app uses simple_form. Thanks so much.

该应用程序使用 simple_form。非常感谢。

回答by gk0r

I've encountered a similar problem, thankfully, there is a simple resolution.

我遇到了类似的问题,谢天谢地,有一个简单的解决方案。

The basic issue is that if you use :disabled => truewith simple_form you will not see that value back in the controller. When you pass an object from HTML form to later bind it to the model - you need all of those attributes. The :disabled => truehowever does not pass any such attribute.

基本问题是,如果您:disabled => true与 simple_form 一起使用,您将不会在控制器中看到该值。当您从 HTML 表单传递对象以稍后将其绑定到模型时 - 您需要所有这些属性。该:disabled => true但是没有通过任何这样的属性。

The solution to this is to use :readonly => true- it will protect the field from user entry and it will still pass the param value back to the controller so you can bind everything to your model.

对此的解决方案是使用:readonly => true- 它会保护该字段免受用户输入,并且它仍会将参数值传递回控制器,以便您可以将所有内容绑定到您的模型。

Good luck.

祝你好运。

See https://github.com/plataformatec/simple_form/pull/367

https://github.com/plataformatec/simple_form/pull/367

回答by Matthew Lehner

I believe you'd just pass in :disabled => true. It's been my experience that options 'just work' with simple_form. So in your case:

我相信你会通过的:disabled => true。根据我的经验,选项“仅适用于”simple_form。所以在你的情况下:

<% @quote.test_items.each do |t| %> 
  <%= f.association :test_items, :label => false, :disabled => true, :selected => t.id %>
<% end %>

From the simple_form github repo:

simple_form github repo

It is also possible to give the :disabled option to SimpleForm, and it'll automatically mark the wrapper as disabled with a css class, so you can style labels, hints and other components inside the wrapper as well.

也可以为 SimpleForm 提供 :disabled 选项,它会自动使用 css 类将包装器标记为禁用,因此您也可以在包装器中设置标签、提示和其他组件的样式。

回答by Axil

The top answers above are all wrong.

上面的答案都是错误的。

disabled attribute has a different behaviour than readonly.

disabled 属性与 readonly 具有不同的行为。

read and compare them:

阅读并比较它们:

http://www.w3schools.com/tags/att_input_disabled.asp

http://www.w3schools.com/tags/att_input_disabled.asp

Tip: Disabled elements in a form will not be submitted.

提示:表单中禁用的元素将不会被提交。

http://www.w3schools.com/tags/att_input_readonly.asp

http://www.w3schools.com/tags/att_input_readonly.asp

The right answer is to use

正确答案是使用

:readonly => true

something like this:

像这样:

<%= f.association :test_items, :label => false, :readonly => true, :selected => t.id %>

回答by jordanpg

It's not clear to me if the association method accepts HTML options or not, but if it does, you can pass disabled: 'disable'to make it read-only with a fixed value.

我不清楚关联方法是否接受 HTML 选项,但如果接受,您可以disabled: 'disable'使用固定值传递使其只读。

I think you might be able to choose the fixed value by passing association as block, as shown in the association docs:

我认为您可以通过将关联作为块传递来选择固定值,如关联文档中所示:

f.association :company do |c|
  c.input :name, selected: 'selection'
  c.input :type
end

As to whether or not the entire list can be read-only and still drop-down, the only solutions I see from google involve JS, for example:

至于整个列表是否可以只读还可以下拉,我从google看到的唯一解决方案涉及JS,例如:

http://techeyes.blogspot.com/2007/11/making-html-select-readonly.html

http://techeyes.blogspot.com/2007/11/making-html-select-readonly.html

回答by Jay H.

Yes, what @gk0r said, as it is documented here:

是的,@gk0r 所说的,正如此处记录的那样

NOTE: The HTMLoptions disabled, readonly, and multiplecan all be treated as booleans. So specifying :disabled => truewill give disabled="disabled".

注意:HTMLoptions disabledreadonlymultiple都可以被视为布尔值。所以指定:disabled => true会给disabled="disabled".

*disabled will have slightly different behavior than readonly.

*disabled 的行为与 readonly 略有不同。