2015-10-12 27 views
1

我一直在試圖讓這個東西工作一段時間,現在似乎沒有任何東西,使它的工作,我已經嘗試了很多東西從可用的例子,並沒有一個工作。我不確定我的控制器上發生了什麼,它具有正確的強參數,並且表格具有正確的屬性。Rails的checbox布爾值將不會插入任何東西到數據庫中

反正這裏是我的代碼:

def survey_params 
    params.require(:survey).permit(:name, :status, :user_id, 
     questions_attributes:[:id, :question_content, :survey_id]) 
end 

有問題的屬性狀態,它似乎並不想用它時,我把它放在我的表格。這裏是我的形式:

<%= form_for(@survey) do |f| %> 
    <% if @survey.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@survey.errors.count, "error") %> prohibited this survey from being saved:</h2> 

     <ul> 
     <% @survey.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :name %><br> 
    <%= f.text_field :name %> 
    </div> 
    <%= f.fields_for :questions do |builder| %> 
    <%= render 'question_fields', f: builder %> 
    <% end %> 
    <div class="field"> 
    <%= f.check_box :status%> 
    <%= f.label :status, "Publish(only check this if you are sure 
    this is your final version of the survey)"%> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

相關部分是這樣的:

<div class="field"> 
     <%= f.check_box :status%> 
     <%= f.label :status, "Publish(only check this if you are sure 
     this is your final version of the survey)"%> 
</div> 

我還相信,這是在測量模型:

class Survey < ActiveRecord::Base 
    belongs_to :user 
    has_many :questions, dependent: :destroy 

    validates_presence_of :name 
    accepts_nested_attributes_for :questions 
end 

所以我不明白爲什麼它不起作用,因爲所有的東西都是這樣設置的。我也嘗試使用check_box_tag(:status)它也沒有工作。它應該在數據庫中插入1或0。不知道這是否已經發生或者沒有按照它的設置方式發生,我看到一些人說他們插入真或假,但是數據庫中的布爾(據我所知)只是1或0。

+1

布爾列通常允許「TRUE」,「FALSE」或「NULL」,不僅僅是簡單的1和0。 –

+0

好的,這很好,除了我現在所擁有的是一列整數而不是布爾值。也許如果我改變它這個工作或什麼? – Argus

回答

0

想通了,只是改變了狀態欄從t.integert.boolean

離開這裏我遷移的這個例子中,以防萬一有人運行到這個問題。

類CreateSurveys <的ActiveRecord ::遷移

def change 
    create_table :surveys do |t| 
     t.string :name 
     t.boolean :status 
     t.references :user, index: true, foreign_key: true 

     t.timestamps null: false 
    end 
    end 
end 

感謝OGZ的澄清嚮導,幫助我找到答案。

相關問題