Reputation: 11
I'm a RoR newbie trying to create a form. I want to be able to save all of the values from the form and run a search query on my database based on the values before returning an updated view. I'm on rails 4, and created an array of check boxes using this tutorial: https://www.youtube.com/watch?v=c2qwV0B9yfU, but the check boxes won't show up. I can't seem to find a solution by myself, so I was hoping for some help.
This is my form.html.erb. It's saved in views/homepage/form.html.erb
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@homepage, url: form_path) do |f| %>
# <%= render 'shared/error_messages_form' %>
<%= f.label :dname, "Dish Name" %>
<%= f.text_field :d_name %>
<%= f.label :rname, "Restaurant Name" %>
<%= f.text_field :r_name %>
<div class="diet">
<%= f.label :violation_name, "Dietary Restriction0.5" %>
<%= hidden_field_tag "homepage[dietaryviolation_ids][]", nil %>
<% Dietaryviolation.all.each do |dietaryviolation| %>
<%= check_box_tag "homepage[dietaryviolation_ids][]", dietaryviolation.id, @homepage.dietaryviolation_ids.include?(dietaryviolation.diet), id: dom_id(dietaryviolation) %>
<%= label_tag dom_id(dietaryviolation), dietaryviolation.diet %><br>
<% end %>
</div>
<div class="food">
<%= f.label :food_name, "Restaurant Cuisine0.5" %>
<%= hidden_field_tag "homepage[restaurantcuisine_ids][]", nil %>
<% @restaurantcuisines.each do |restaurantcuisine| %>
<%= f.check_box "homepage[restaurantcuisine_ids][]", restaurantcuisine.id, @homepage.restaurantcuisine_ids.include?(restaurantcuisine.diet), id: dom_id(restaurantcuisine) %>
<%= label_tag dom_id(restaurantcuisine), restaurantcuisine.diet %><br>
<% end %>
</div>
<%= f.label "Calories"%>
<%= form_for :homepage, url: form_path do |cal| %>
<div id="calorie_slider"></div>
<%= cal.hidden_field :c_min %>
<%= cal.hidden_field :c_max %>
<% end %>
<%= f.label "Rating"%>
<%= form_for :homepage, url: form_path do |rate| %>
<div id="rating_slider"></div>
<%= rate.hidden_field :r_min %>
<%= rate.hidden_field :r_max %>
<% end %>
<%= f.label "Price Range"%>
<%= form_for :homepage, url: form_path do |price| %>
<div id="price_slider"></div>
<%= price.hidden_field :p_min %>
<%= price.hidden_field :p_max %>
<% end %>
<%= f.submit "Filter my shiz!", class: "btn btn-primary" %>
<% end %>
The corresponding HTML for the check_box_tag section looks like this:
<div class="diet">
<label for="homepage_violation_name">Dietary Restriction0.5</label>
<input type="hidden" name="homepage[dietaryviolation_ids][]" id="homepage_dietaryviolation_ids_" />
</div>
<div class="food">
<label for="homepage_food_name">Restaurant Cuisine0.5</label>
<input type="hidden" name="homepage[restaurantcuisine_ids][]" id="homepage_restaurantcuisine_ids_" />
</div>
It seems to be missing the check boxes entirely, so I think this is where the error is but I have no idea why it's happening :(
The corresponding controller is homepage_controller.rb
class HomepageController < ApplicationController
def index
end
def new
@homepage = Homepage.new
end
def form
@restaurantcuisines = Restaurantcuisine.select(:cuisine).distinct
@dietaryviolations = Dietaryviolation.select(:diet).distinct
@homepage = Homepage.new(homepage_params)
end
def filter
end
private
def homepage_params
params.permit(:d_name, :r_name, :c_min, :c_max, :r_min, :r_max, :p_min, :p_max, { :restaurantcuisine_ids => [] }, { :dietaryviolation_ids => [] } )
end
end
I have created CRUD functions for restaurantscuisine_controller and dietaryviolations_controller, but nothing else.
The homepage model is linked to the dietaryviolation model and the restaurantscuisine model through a has-and-belongs-to-many relation. The relations are brought together in join tables called dietaryviolations_homepages and homepages_restaurantscuisine. I don't have anything else in the models except for the has_and_belongs_to_many line and some validation checks in Homepage
class Homepage < ActiveRecord::Base
has_and_belongs_to_many :restaurantcuisine
has_and_belongs_to_many :dietaryviolation
before_save { self.d_name = d_name.downcase } if ':d_name.present?'
before_save { self.r_name = r_name.downcase } if ':r_name.present?'
validates :c_max, numericality: { only_integer: true, less_than_or_equal_to: 5000, greater_than_or_equal_to: :c_min }
validates :c_min, numericality: { only_integer: true, less_than_or_equal_to: :c_max, greater_than_or_equal_to: 0 }
validates :r_max, numericality: { only_integer: true, less_than_or_equal_to: 5, greater_than_or_equal_to: :r_min }
validates :r_min, numericality: { only_integer: true, less_than_or_equal_to: :r_max, greater_than_or_equal_to: 0 }
validates :p_max, numericality: { only_integer: true, less_than_or_equal_to: 50, greater_than_or_equal_to: :p_min }
validates :p_min, numericality: { only_integer: true, less_than_or_equal_to: :p_max, greater_than_or_equal_to: 0 }
validates_associated :restaurantecuisine
validates_associated :dietaryviolation
end
My application.scss file has the following lines:
*= require jquery-ui
*= require bootstrap-slider
*= require_tree .
*= require_self
*/
@import "bootstrap-sprockets"
@import "bootstrap"
@import url(http://www.w3schools.com/lib/w3.css)
My application.js file has the following lines:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require bootstrap
//= require bootstrap-sprockets
//= require bootstrap-slider
//= require turbolinks
//= require_tree .
Any advice would be appreciated! I've tried a bunch of different ways to make the check boxes work, including using collection_check_box but nothing seems to be working out.
Upvotes: 1
Views: 775
Reputation: 495
Try doing this instead,
<%= f.collection_check_boxes :foo, Foo.all, :id, :name do |cb| %>
<% cb.label(class: "checkbox-inline input_checkbox") {cb.check_box(class: "checkbox") + cb.text} %>
<% end %>
Do replace :foo
with the appropriate object names. The classes are meant for css which can be ignored.
Also, just a little explanation as to what is going on.
collection_check_boxes(object, method, value_method, text_method)
As taken from here, so we have the :foo
object, the method being Foo.all
, similar to when you do <% @restaurantcuisines.each do |restaurantcuisine| %>
. Then we have :id
which is the value_method
and lastly our label
uses text_method
which is :name
. Do note, of course if your model does not have a :name
column, you obviously cannot use :name
for your text_method
.
I would also like to note, in forms whether is it text_area, etc, it will be input_method :object
.
Upvotes: 1