Rails Check Box - Allowing User To Choose Days Off The Week -


i have model name "deal" , string called "days". want able allow users choose number of days deal available. 1 day (monday), or multiple days (monday, wednesday, friday...) how can this?

i tried this:

<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> 

but overwrites first option showing "tuesday".

do have add string model everyday of week, or there easier option? thanks.

deals_controller:

class dealscontroller < applicationcontroller  def index   @deals = deal.all    respond_to |format|     format.html # index.html.erb     format.json { render json: @deals }   end end  def show   @deal = deal.find(params[:id])    respond_to |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 |format|     if @deal.save       format.html { redirect_to @deal, notice: 'deal 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 

deal.rb

class deal < activerecord::base     belongs_to :user         attr_accessible :title, :description, :days end 

if i'm understanding correctly, you'll want create :day attribute (strings easiest), , use radio buttons (rather checkboxes) allow users choose 1 of options.

your view this:

<div class="field">     <%= f.label :day, "choose day:" %>     <%= f.radio_button :day, "monday", "tuesday", "wednesday" %> </div> 

alternately, use select tag, this:

<%= f.select_tag(:day, [['monday', 'monday'], ['tuesday', 'tuesday], ...]) %> 

am answering right question?

edit hmm. never tried before. if want user able select several days, might easiest create bunch of attr_accessor variables each day, create checkbox each of those, , have controller or before_validate action in model update :days attribute in model (an array) add of checked days array. messy version of model method this:

attr_accessible :days, ... attr_accessor :monday, :tuesday, :wednesday...  before_validation :update_days  def update_days   day_array = []    day_array << "monday" if self.monday == true   day_array << "tuesday" if self.tuesday == true   ...    self.update_attribute(:days, day_array) end 

and then, in view, separate checkboxes each temporary attribute:

<div class="field">     <%= f.label :monday %>     <%= f.check_box :monday %> </div> <div class="field">     <%= f.label :tuesday %>     <%= f.check_box :tuesday %> </div> 

you create days helper in application_helper array of day symbols, like:

<% days_of_the_week.each |day| %>     <div class="field">         <%= f.label day %>         <%= f.check_box day %>     </div> <% end %> 

to avoid repetition.

again, not cleanest way, think work, without having other change days attribute array.


Comments