Rails not editable text field

Ruby on-RailsTextfieldForm For

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Make it readonly !

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

Solution 2 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoeView Question on Stackoverflow
Solution 1 - Ruby on-Railskrunal shahView Answer on Stackoverflow
Solution 2 - Ruby on-RailsmshasibView Answer on Stackoverflow