我有一个模型名称“Deal”和一个名为“Days”的字符串。我希望能够允许我的用户选择优惠可用的天数。可能是一天(星期一),也可能是多天(星期一、星期三、星期五...) 我该怎么做?
我试过这个:
<div class="field">
<%= f.label :monday %>
<%= f.check_box :days, {}, "monday", "monday" %>
</div>
<div class="field">
<%= f.label :tuesday %>
<%= f.check_box :days, {}, "tuesday", "tuesday" %>
</div>
但它会覆盖仅显示“星期二”的第一个选项。
我是否必须为一周中的每一天向模型添加一个字符串,还是有更简单的选择?谢谢。
交易控制器:
class DealsController < ApplicationController
def index
@deals = Deal.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @deals }
end
end
def show
@deal = Deal.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @deal }
end
end
def create
@deal = Deal.new(params[:deal])
@deal.created_at = Time.now
@deal.user = current_user
respond_to do |format|
if @deal.save
format.html { redirect_to @deal, notice: 'Deal was successfully created.' }
format.json { render json: @deal, status: :created, location: @deal }
else
format.html { render action: "new" }
format.json { render json: @deal.errors, status: :unprocessable_entity }
end
end
end
end
交易.rb
class Deal < ActiveRecord::Base
belongs_to :user
attr_accessible :title, :description, :days
end