Rails not editable text field

荒凉一梦 提交于 2019-12-04 07:48:53

问题


I have a form_for written in the following manner:

<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

TNX


回答1:


Make it readonly !

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



回答2:


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) %>


来源:https://stackoverflow.com/questions/5620128/rails-not-editable-text-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!