2015-10-12 22 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的澄清向导,帮助我找到答案。

相关问题