私はフォームを作成しようとしているRoR初心者です。私は、フォームからすべての値を保存し、更新されたビューを返す前に値に基づいて自分のデータベースで検索クエリを実行できるようにしたい。私はレール4にあり、このチュートリアル:https://www.youtube.com/watch?v=c2qwV0B9yfUを使用して一連のチェックボックスを作成しましたが、チェックボックスは表示されません。私は自分で解決策を見つけることができないので、私はいくつかの助けを望んでいた。Ruby on rails 4 check_box_tagがフォームに表示されない
これは私のform.html.erbです。これは、ビュー/ホームページ/ 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 %>
check_box_tagセクションの対応するHTMLは次のようになります。
<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>
それはそうですチェックボックスが完全に欠落しているため、エラーが発生している場所だと思いますが、なぜそれが問題なのかわかりません
:(対応するコントローラが起こっていることは、私がrestaurantscuisine_controllerとdietaryviolations_controller、それ以外何のためにCRUD機能を作成しました
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
homepage_controller.rbです。
ホームページのモデルは、有益性のモデルとレストランのモデルとに、has-and-belong-to-manyの関係でリンクされています。リレーションシップは、dietaryviolations_homepagesおよびhomepages_restaurantscuisineと呼ばれる結合表にまとめられています。
*= require jquery-ui
*= require bootstrap-slider
*= require_tree .
*= require_self
*/
@import "bootstrap-sprockets"
@import "bootstrap"
@import url(http://www.w3schools.com/lib/w3.css)
ホームページに
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
をhas_and_belongs_to_manyアソシエーションライン以外のモデルでは、何といくつかの検証チェックを持っていません
私のapplication.jsファイルには、次の行があります。
アドバイスありがとうございます。私はcollection_check_boxを使うことを含め、チェックボックスを動作させるためにさまざまな方法を試しましたが、何も問題がないようです。
Foo.allにはfoo、@dietaryviolations、:idと:nameにはdiet:for:idまたは:name列がないので、ダイエットの列は基本的に名前です、食物拘束に違反する特定の成分である成分欄)が表示されません。 – tintin
':id'は間違いなくそこにあります。すべてのテーブルには自動的に 'id'カラムがあります。それを追加して、それが動作するかどうかを確認してください。 – angkiki