У меня есть атрибут под названием «функции» в моем приложении. В моей форме «функции» состоит из списка флажков. Идея заключается в том, что пользователи могут отмечать, какие «функции» применяются к их сообщению, и этот список функций сохраняется в записи.
Я вижу, что массив сохраняется в моей консоли ( "features" => {"Private bathroom" => "1", "Elevator" => "0", "Complementary Breakfast" => "1" , "Great view" => "1", "Cable TV" => "0", "Fireplace" => "0", "Other (см. Описание)" => "0", "Sweet location" => " 0" } ).
Однако ... Когда я просматриваю запись, функция возвращает ноль. Кажется, он не сохраняет массив функций.
Код указан ниже. Есть идеи, что я тут не так делаю?
Модели / accommodation.rb
class Accommodation < 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 => true
end
Контроллеры / accommodation_controller.rb
class AccommodationsController < ApplicationController
before_filter :auth, :except => :show
uses_tiny_mce ( :options => {
:theme => 'advanced',
:theme_advanced_toolbar_location => 'top',
:theme_advanced_toolbar_align => 'left',
:theme_advanced_buttons1 => 'bold,italic,underline,image,bullist,numlist,separator,undo,redo',
:theme_advanced_buttons2 => '',
:theme_advanced_buttons3 => ''
})
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' && 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 => 'show'
end
end
return true
else
flash[:error] = "Please login first."
redirect_to :controller => 'sessions', :action => 'new'
end
end
end
Вид / размещение / _form.html.erb
<%= form_for @accommodation, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<p>
Title<br />
<%= f.text_field :title, :size => 60 %>
</p>
<p>
Description<br />
<%= f.text_area :description, :rows => 17, :cols => 75, :class => "mceEditor" %>
</p>
[...snip...]
<p>
<i>Featuring...</i>
<%= fields_for :features do |feature_fields| %>
<table>
<tr>
<td><%= feature_fields.check_box 'Private bathroom' %> Private bathroom</td>
<td><%= feature_fields.check_box 'Cable TV' %> Cable TV</td>
<td><%= feature_fields.check_box 'Complimentary breakfast' %> Complimentary breakfast</td>
</tr>
<tr>
<td><%= feature_fields.check_box 'Elevator' %> Elevator</td>
<td><%= feature_fields.check_box 'Fireplace' %> Fireplace</td>
<td><%= feature_fields.check_box 'Great view' %> Great view</td>
</tr>
<tr>
<td><%= feature_fields.check_box 'Sweet location' %> Sweet location</td>
<td><%= feature_fields.check_box 'Other (see description)' %> Other (see description)</td>
</tr>
</table>
<% end %>
</p>
[...snip...]
<% end %>