Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring Array of Checkbox Selections
    primarykey
    data
    text
    <p>I have an attribute called "features" in my application. In my form, "features" consists of a list of check boxes. The idea here is that users can check off which "features" apply to their post, and that list of features gets saved into the record.</p> <p>I see the array being saved in my console (<i>"features"=>{"Private bathroom"=>"1", "Elevator"=>"0", "Complimentary breakfast"=>"1", "Great view"=>"1", "Cable TV"=>"0", "Fireplace"=>"0", "Other (see description)"=>"0", "Sweet location"=>"0"}</i>).</p> <p>However... When I view the record, features returns nil. It doesn't seem to be saving the features array.</p> <p>Code provided below. Any idea what I'm doing wrong here?</p> <p><strong>models/accommodation.rb</strong></p> <pre><code>class Accommodation &lt; ActiveRecord::Base validates_presence_of :title, :description, :thing, :location attr_accessible :photo_attributes, :title, :description, :thing, :borough, :location, :spaces, :price, :features has_one :photo has_many :requests belongs_to :user accepts_nested_attributes_for :photo, :allow_destroy =&gt; true end </code></pre> <p><strong>controllers/accommodation_controller.rb</strong></p> <pre><code>class AccommodationsController &lt; ApplicationController before_filter :auth, :except =&gt; :show uses_tiny_mce ( :options =&gt; { :theme =&gt; 'advanced', :theme_advanced_toolbar_location =&gt; 'top', :theme_advanced_toolbar_align =&gt; 'left', :theme_advanced_buttons1 =&gt; 'bold,italic,underline,image,bullist,numlist,separator,undo,redo', :theme_advanced_buttons2 =&gt; '', :theme_advanced_buttons3 =&gt; '' }) def show @accommodation = Accommodation.find(params[:id]) end def new @accommodation = current_user.accommodations.build @accommodation.build_photo end def create @accommodation = current_user.accommodations.build(params[:accommodation]) if @accommodation.save flash[:notice] = "Successfully created your accommodation." redirect_to @accommodation else render :new end end def edit @accommodation = Accommodation.find(params[:id]) end def update @accommodation = Accommodation.find(params[:id]) if @accommodation.update_attributes(params[:accommodation]) flash[:notice] = "Successfully updated accommodation." redirect_to @accommodation else render :edit end end def destroy @accommodation = Accommodation.find(params[:id]) @accommodation.destroy flash[:notice] = "Successfully destroyed accommodation." redirect_to :inkeep end private def auth if current_user if params[:action] != 'new' &amp;&amp; params[:action] != 'create' @accommodation = Accommodation.find(params[:id]) if @accommodation.user_id != current_user.id flash[:notice] = "You don't own this accommodation!" render :action =&gt; 'show' end end return true else flash[:error] = "Please login first." redirect_to :controller =&gt; 'sessions', :action =&gt; 'new' end end end </code></pre> <p><strong>views/accommodations/_form.html.erb</strong></p> <pre><code>&lt;%= form_for @accommodation, :html =&gt; {:multipart =&gt; true} do |f| %&gt; &lt;%= f.error_messages %&gt; &lt;p&gt; Title&lt;br /&gt; &lt;%= f.text_field :title, :size =&gt; 60 %&gt; &lt;/p&gt; &lt;p&gt; Description&lt;br /&gt; &lt;%= f.text_area :description, :rows =&gt; 17, :cols =&gt; 75, :class =&gt; "mceEditor" %&gt; &lt;/p&gt; [...snip...] &lt;p&gt; &lt;i&gt;Featuring...&lt;/i&gt; &lt;%= fields_for :features do |feature_fields| %&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;&lt;%= feature_fields.check_box 'Private bathroom' %&gt; Private bathroom&lt;/td&gt; &lt;td&gt;&lt;%= feature_fields.check_box 'Cable TV' %&gt; Cable TV&lt;/td&gt; &lt;td&gt;&lt;%= feature_fields.check_box 'Complimentary breakfast' %&gt; Complimentary breakfast&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;%= feature_fields.check_box 'Elevator' %&gt; Elevator&lt;/td&gt; &lt;td&gt;&lt;%= feature_fields.check_box 'Fireplace' %&gt; Fireplace&lt;/td&gt; &lt;td&gt;&lt;%= feature_fields.check_box 'Great view' %&gt; Great view&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;%= feature_fields.check_box 'Sweet location' %&gt; Sweet location&lt;/td&gt; &lt;td&gt;&lt;%= feature_fields.check_box 'Other (see description)' %&gt; Other (see description)&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;% end %&gt; &lt;/p&gt; [...snip...] &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload