form_for字段中的值

时间:2016-07-07 09:50:39

标签: ruby-on-rails forms

我试图像这样设置表单字段的值:

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

在视图中,我可以在字段中看到id,但是当我尝试提交表单时,我发现验证没有通过。验证看起来像这样:

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

在我的架构中,列city_id设置为integer。我还尝试将f.text_field更改为f.number_field,但它也没有任何帮助。那么,有什么不对的?谢谢。

1 个答案:

答案 0 :(得分:2)

我认为您应该使用readonly: true代替disabled。由于disabled不会将您的数据传递给服务器。

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