我的应用程序中有一个名为“features”的属性。在我的表单中,“features”包含一个复选框列表。这里的想法是用户可以检查哪些“功能”适用于他们的帖子,并且该功能列表会保存到记录中。
我看到数组正在我的控制台中保存(“features”=> {“私人浴室”=>“1”,“电梯”=>“0”,“免费早餐”=> ;“1”,“Great view”=>“1”,“有线电视”=>“0”,“壁炉”=>“0”,“其他(见说明)”=>“0” ,“Sweet location”=>“0”} )。
然而......当我查看记录时,功能返回nil。它似乎没有保存features数组。
以下提供的代码。知道我在这里做错了吗?
模型/ 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 %>
答案 0 :(得分:3)
首先,在params散列中你的住宿哈希中的features数组是什么?
其次,没有接受数组的db列类型,因此需要输入
serialize :features
在模型中。这会将数组存储为db中的yaml。您还可以将数据类型指定为serialize()的参数(在本例中可能是Array),但并不总是必要。
答案 1 :(得分:1)
我今天添加了同样的问题,看来表单未在视图中正确构建。
确实,仔细看看你的参数:params[:features]
在params[:accomodation]
我只是在创建操作开始时添加:
params[:accomodation][:features] = params[:features]
它运作正常
答案 2 :(得分:0)
你的模特怎么样?你有没有attr_accessible或attr_protected调用?