Home Rails 5 form_for with checkbox array
Reply: 0

Rails 5 form_for with checkbox array

user2051 Published in July 22, 2018, 8:35 pm

I am using a PostgreSQL database and Rails 5.0.6. I try to build a course allocation WebApp for the school where I am working. For each course the teachers are able to select which forms are allowed to visit the course.

Migration file:

  def up
    create_table :courses do |t|
      t.integer :number, null: false
      t.string :name, null: false
      t.text :description, null: false
      t.decimal :level, array: true, default: []
      t.integer :max_visitor, null: false
      t.integer :min_visitor
      t.integer :pos_visit

In my Controller:

    params.require(:course).permit(:number, :name, :description, :level [], :max_visitor, :min_visitor, :pos_visits)

I already read this post: Rails 5 strong params with an Array within check boxes values. But the Syntax params.require(:product).permit(:id, **category_ids: []**) doesnt work for me even though I am using rails 5 as well. I am not sure if :level [] really works but it seems to be correct syntax.

This is my Form:

<%= form_for @course do |t| %>
            <%= t.text_field :number, class: 'form-control' %>
            <%= t.text_field :name, class: 'form-control' %>
            <%= t.text_area :description, class: 'form-control' %>
            <%= t.check_box :level[], 1%>
            <%= t.check_box :level[], 2%>
            <%= t.check_box :level[], 3%>
            <%= t.check_box :level[], 4%>
            <%= t.text_field :max_visitor, class: 'form-control' %>
            <%= t.text_field :min_visitor, class: 'form-control' %>
            <%= t.text_field :pos_visit, class: 'form-control' %><br/>
        <%= t.submit "best├Ątigen", class: "btn btn-success"%>
<% end %>

This check_box syntax seems to be wrong. May anyone help me with the right syntax of the check_boxes? Thanks for help.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308145 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO