Ruby-on-rails Rails 不可编辑的文本字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5620128/
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 00:53:50  来源:igfitidea点击:

Rails not editable text field

ruby-on-railstextfieldform-for

提问by Joe

I have a form_for written in the following manner:

我有一个以下列方式编写的 form_for :

<div class="field">
    <%= location.label :city %>
    <%= location.text_field :city, :disabled=>true%>
</div>
<div class="field">
    <%= location.label :country %>
    <%= location.text_field :country, :disabled=>true%>
</div>

As you can see the 2 textfield are disabled because they are autofilled by a jquery function and I don't want let the user handle them. The problem is that in this way, the view doesen't pass that parameters to the controller because are disabled !!! Is there any other way to pass not editable text_field to the controller, taking care that I don't want to use hidden field because I want to show the results to the user inside a textbox

正如您所看到的,2 个文本字段被禁用,因为它们是由 jquery 函数自动填充的,我不想让用户处理它们。问题是,通过这种方式,视图不会将该参数传递给控制器​​,因为被禁用了!!!有没有其他方法可以将不可编辑的 text_field 传递给控制器​​,注意我不想使用隐藏字段,因为我想在文本框中向用户显示结果

TNX

TNX

回答by krunal shah

Make it readonly !

让它只读!

<%= location.text_field :country,:readonly => true%>

回答by mshasib

The trick is to use "object" in conjunction with a label for anything you don't want to change. Here is how you should code it:

诀窍是将“对象”与标签一起用于您不想更改的任何内容。以下是您应该如何对其进行编码:

<%= location.label(:country, f.object.country) %>