如何在text_field中设置值并将这些值存储到rails中的数据库表中

时间:2016-11-03 12:39:59

标签: mysql ruby ruby-on-rails-3

这些东西我已经准备好了,但是当时text_field的值没有插入到数据库表中,扩孔值插入成功但是没有插入一个值。

我可以理解我错在哪里以及缺少什么......

_employee_details.html.erb

<%= form_for(:employee_details, :url => {:controller => 'hr', :action => 'create'}) do |f| %>
       <%= f.text_field :employee_id, { :value=> "123548", :disabled=>true , :required => true, placeholder: 'E12345678', class: 'form-control' } %>
<% end %>

hr_controller.rb

class HrController < ApplicationController

    def new
        @employees = EmployeeDetail.new
    end

    def create
        @employees = EmployeeDetail.new(employee_params)
        if @employees.save
            redirect_to :action => 'internal_employee_page'
        else
            redirect_to :action => 'internal_employee_page'
        end
    end

    private

        def employee_params
             params.require(:employee_details).permit(:offer_letter_id, :employee_id, :bank_ac, :bank_ifsc, :spouse_name, :gender, :work_end_date)
        end     
end

所有值都成功插入employee_details表接受 employee_id

employee_details.rb 这是我的模态

class EmployeeDetail < ActiveRecord::Base 
    belongs_to :user
    validates :offer_letter_id, presence: true 
end

我已经尝试过了,但这不起作用

1 个答案:

答案 0 :(得分:0)

已停用的输入不会提交数据。

而不是禁用使用只读属性:

<%= f.text_field :employee_id, { :value=> "123548", :readonly => true , :required => true, placeholder: 'E12345678', class: 'form-control' } %>

<%= form_for(:employee_details, :url => {:controller => 'hr', :action => 'create'}) do |f| %>
       <%= f.text_field :employee_id, { :value=> "123548", :readonly => true , :required => true, placeholder: 'E12345678', class: 'form-control' } %>
<% end %>

Here is the documentation of it