ruby - How to add multiple checkbox array field in rails application and showing on view page? -


i have made rails application in have made option of multiple checkboxes , stores entries in array. things not working out. here code: showing nonameerror undefined method `each' nil:nilclass on line in show page <%= @branches.each |t| %> new form:

  </p>   <%= f.label :allowedbranches %><br>   <%= check_box_tag  :allowedbranches,{}, value='coe'%><%= f.label :coe %><br>   <%= check_box_tag  :allowedbranches,{}, value='ece'%><%= f.label :ece %><br>   <%= check_box_tag  :allowedbranches,{}, value='ice'%><%= f.label :ice %><br>   <%= check_box_tag  :allowedbranches,{}, value='it'%><%= f.label :it %><br>   <%= check_box_tag  :allowedbranches,{}, value='mpae'%><%= f.label :mpae %><br>   <%= check_box_tag  :allowedbranches,{}, value='bt'%><%= f.label :bt %><br>   <%= check_box_tag  :allowedbranches,{}, value='is'%><%= f.label :is %><br>   <%= check_box_tag  :allowedbranches,{}, value='sp'%><%= f.label :sp %><br>   <%= check_box_tag  :allowedbranches,{}, value='pc'%><%= f.label :pc %><br>   <p> 

controller:

def new     @company = company.new end  def edit      @company = company.find(params[:id]) end  def update     @company = company.find(params[:id])     if @company.update(company_params)         redirect_to @company     else         render 'edit'     end end  def create      @company = company.new(company_params)      if @company.save         redirect_to @company     else         render 'new'     end end  def index     @companies = company.all end def destroy     @company = company.find(params[:id])     @company.destroy      redirect_to companies_path end  def show     @company = company.find(params[:id])     @branches = @company.collect(:allowedbranches) end private     def company_params          params.require(:company).permit(:name, :becutoff,:grade,:xiicutoff,:xcutoff,:backsallowed,:details,:package,:deadline,allowedbranches:[]) if params[:company]     end 

show page:

<p> <strong>allowed branches:</strong> <%= @branches.each |t| %>  <%= puts t %> <% end %> </p> 

i want whenever edit form, same checkboxes checked.

logs are

started patch "/companies/15" 127.0.0.1 @ 2014-06-28 00:54:09 +0530 processing companiescontroller#update html   parameters: {"utf8"=>"✓", "authenticity_token"=>"92w4xjiurfyvtwrmawnmqleddszkhe6szmq0ghtrfay=", "company"=>{"name"=>"facebook", "grade"=>"dream", "becutoff"=>"70.0", "backsallowed"=>"0", "details"=>"apply online", "package"=>"93.0", "xiicutoff"=>"60.0", "xcutoff"=>"60.0", "deadline"=>"2014-06-29 00:50:45"}, "allowedbranches"=>"{}", "commit"=>"update company", "id"=>"15"}   [1m[36mcompany load (5.0ms)[0m  [1mselect  "companies".* "companies"  "companies"."id" = $1 limit 1[0m  [["id", 15]]   [1m[35m (2.0ms)[0m  begin   [1m[36m (3.0ms)[0m  [1mcommit[0m redirected http://localhost:3000/companies/15 completed 302 found in 65ms (activerecord: 10.0ms)   started "/companies/15" 127.0.0.1 @ 2014-06-28 00:54:09 +0530 processing companiescontroller#show html   parameters: {"id"=>"15"}   [1m[35mcompany load (2.0ms)[0m  select  "companies".* "companies"  "companies"."id" = $1 limit 1  [["id", 15]]   rendered companies/show.html.erb within layouts/application (5.0ms) completed 200 ok in 431ms (views: 418.0ms | activerecord: 2.0ms)   started "/assets/bootstrap-theme.css?body=1" 127.0.0.1 @ 2014-06-28 00:54:10 +0530   started "/assets/bootstrap-theme.min.css?body=1" 127.0.0.1 @ 2014-06-28 00:54:11 +0530   started "/assets/bootstrap.min.css?body=1" 127.0.0.1 @ 2014-06-28 00:54:11 +0530   started "/assets/bootstrap.css?body=1" 127.0.0.1 @ 2014-06-28 00:54:11 +0530   started "/assets/companies.css?body=1" 127.0.0.1 @ 2014-06-28 00:54:11 +0530   started "/assets/students.css?body=1" 127.0.0.1 @ 2014-06-28 00:54:11 +0530   started "/assets/welcome.css?body=1" 127.0.0.1 @ 2014-06-28 00:54:11 +0530   started "/assets/application.css?body=1" 127.0.0.1 @ 2014-06-28 00:54:11 +0530   started "/assets/jquery.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:11 +0530   started "/assets/jquery_ujs.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:11 +0530   started "/assets/turbolinks.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:12 +0530   started "/assets/bootstrap.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:12 +0530   started "/assets/bootstrap.min.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:12 +0530   started "/assets/companies.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:12 +0530   started "/assets/locales/bootstrap-datetimepicker.ar-ma.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:12 +0530   started "/assets/locales/bootstrap-datetimepicker.ar.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:12 +0530   started "/assets/locales/bootstrap-datetimepicker.bg.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:12 +0530   started "/assets/locales/bootstrap-datetimepicker.br.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:12 +0530   started "/assets/locales/bootstrap-datetimepicker.bs.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:12 +0530   started "/assets/locales/bootstrap-datetimepicker.by.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:12 +0530   started "/assets/locales/bootstrap-datetimepicker.ca.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:12 +0530   started "/assets/locales/bootstrap-datetimepicker.cs.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:12 +0530   started "/assets/locales/bootstrap-datetimepicker.cv.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:12 +0530   started "/assets/locales/bootstrap-datetimepicker.da.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:13 +0530   started "/assets/locales/bootstrap-datetimepicker.de.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:13 +0530   started "/assets/locales/bootstrap-datetimepicker.el.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:13 +0530   started "/assets/locales/bootstrap-datetimepicker.en-au.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:13 +0530   started "/assets/locales/bootstrap-datetimepicker.en-ca.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:13 +0530   started "/assets/locales/bootstrap-datetimepicker.en-gb.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:13 +0530   started "/assets/locales/bootstrap-datetimepicker.eo.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:13 +0530   started "/assets/locales/bootstrap-datetimepicker.es.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:13 +0530   started "/assets/locales/bootstrap-datetimepicker.et.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:13 +0530   started "/assets/locales/bootstrap-datetimepicker.eu.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:13 +0530   started "/assets/locales/bootstrap-datetimepicker.fa.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:13 +0530   started "/assets/locales/bootstrap-datetimepicker.fi.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:13 +0530   started "/assets/locales/bootstrap-datetimepicker.fr-ca.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:13 +0530   started "/assets/locales/bootstrap-datetimepicker.fr.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:13 +0530   started "/assets/locales/bootstrap-datetimepicker.gl.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:14 +0530   started "/assets/locales/bootstrap-datetimepicker.he.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:14 +0530   started "/assets/locales/bootstrap-datetimepicker.hi.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:14 +0530   started "/assets/locales/bootstrap-datetimepicker.hr.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:14 +0530   started "/assets/locales/bootstrap-datetimepicker.hu.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:14 +0530   started "/assets/locales/bootstrap-datetimepicker.id.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:14 +0530   started "/assets/locales/bootstrap-datetimepicker.is.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:14 +0530   started "/assets/locales/bootstrap-datetimepicker.it.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:14 +0530   started "/assets/locales/bootstrap-datetimepicker.ja.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:14 +0530   started "/assets/locales/bootstrap-datetimepicker.ka.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:14 +0530   started "/assets/locales/bootstrap-datetimepicker.ko.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:14 +0530   started "/assets/locales/bootstrap-datetimepicker.lt.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:14 +0530   started "/assets/locales/bootstrap-datetimepicker.lv.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:14 +0530   started "/assets/locales/bootstrap-datetimepicker.ml.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:14 +0530   started "/assets/locales/bootstrap-datetimepicker.mr.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:15 +0530   started "/assets/locales/bootstrap-datetimepicker.ms-my.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:15 +0530   started "/assets/locales/bootstrap-datetimepicker.nb.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:15 +0530   started "/assets/locales/bootstrap-datetimepicker.ne.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:15 +0530   started "/assets/locales/bootstrap-datetimepicker.nl.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:15 +0530   started "/assets/locales/bootstrap-datetimepicker.nn.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:15 +0530   started "/assets/locales/bootstrap-datetimepicker.pl.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:15 +0530   started "/assets/locales/bootstrap-datetimepicker.pt-br.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:15 +0530   started "/assets/locales/bootstrap-datetimepicker.pt.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:15 +0530   started "/assets/locales/bootstrap-datetimepicker.ro.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:15 +0530   started "/assets/locales/bootstrap-datetimepicker.ru.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:15 +0530   started "/assets/locales/bootstrap-datetimepicker.sk.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:15 +0530   started "/assets/locales/bootstrap-datetimepicker.sl.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:15 +0530   started "/assets/locales/bootstrap-datetimepicker.sq.js?body=1" 127.0.0.1 @ 2014-06-28 00:54:16 +0530 

migration add allowedbranches table companies

class addbanchesallowedarraytocompanies < activerecord::migration   def change     add_column :companies, :allowedbranches, :string, :array=>true   end end 

you have number of problems here.

firstly, you're mixing form form_tag , form_for. difference you're doing do |f| %> @ end of form tag.

what mean is, should have <%= f.check_box ... %> instead of <%= check_box_tag ... %>

secondly, value you're inputting {} each check box , creating value=whatever not part of options check_box_tag. solve this:

  <%= check_box_tag  "allowedbranches[]", 'coe'%><%= label_tag :coe %><br>   <%= check_box_tag  "allowedbranches[]", 'ece'%><%= label_tag :ece %><br>   <%= check_box_tag  "allowedbranches[]", 'ice'%><%= label_tag :ice %><br>   <%= check_box_tag  "allowedbranches[]", 'it'%><%= label_tag :it %><br>   <%= check_box_tag  "allowedbranches[]", 'mpae'%><%= label_tag :mpae %><br>   <%= check_box_tag  "allowedbranches[]", 'bt'%><%= label_tag :bt %><br>   <%= check_box_tag  "allowedbranches[]", 'is'%><%= label_tag :is %><br>   <%= check_box_tag  "allowedbranches[]", 'sp'%><%= label_tag :sp %><br>   <%= check_box_tag  "allowedbranches[]", 'pc'%><%= label_tag :pc %><br> 

which create array params you. can loop through it


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -