Alex Zakruzhetskyi
Alex Zakruzhetskyi

Reputation: 1433

A value in a form_for field

I'm trying to set a value for a form field like this:

<%= form_for (@change_office_address), remote: true, format: :json, html: { class: :contact_form } do |f| %>
  <%= f.text_field :city_id, class: 'form-control', value: @office.city.id, disabled: true %> 
  <%= f.submit, class: 'btn btn-default' %>
<% end %>

In a view I can see the id in a field, but when I tried to submit the form, I see that validation doesn't pass. The validation looks like this:

class ChangeOfficeAddress < ApplicationRecord
  belongs_to :city
  validates :city_id, presence: true
end

In my schema the column city_id is set to integer. I also tried to change f.text_field to f.number_field, but it didn't help either. So, what can be wrong? Thanks ahead.

Upvotes: 1

Views: 1110

Answers (1)

Khanh Pham
Khanh Pham

Reputation: 2973

I think you should use readonly: true instead of disabled. Because of disabled won't pass your data to server.

<%= f.text_field :city_id, class: 'form-control', value: @office.city.id, readonly: true %> 

Upvotes: 2

Related Questions