我是一個試圖創建窗體的RoR新手。我希望能夠保存表單中的所有值,並在返回更新視圖之前根據這些值在我的數據庫上運行搜索查詢。我在rails 4上,並使用本教程創建了一組複選框:https://www.youtube.com/watch?v=c2qwV0B9yfU,但複選框不顯示。我似乎無法自己找到解決方案,所以我希望得到一些幫助。紅寶石軌道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>
似乎完全缺少複選框,所以我認爲這是錯誤的地方,但我不知道爲什麼發生:(
相應的控制器是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
我創建CRUD功能restaurantscuisine_controller和dietaryviolations_controller,但沒有別的。
主頁模型通過has-and-belongs-to-many關係鏈接到dietaryviolation模型和restaurantscuisine模型。這些關係彙集在稱爲dietaryviolations_homepages和homepages_restaurantscuisine的連接表中。我沒有任何東西在別的車型除了在has_and_belongs_to_many線和在首頁的一些驗證檢查
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
我application.scss文件有以下幾行:
*= require jquery-ui
*= require bootstrap-slider
*= require_tree .
*= require_self
*/
@import "bootstrap-sprockets"
@import "bootstrap"
@import url(http://www.w3schools.com/lib/w3.css)
我的application.js文件有以下幾行:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require bootstrap
//= require bootstrap-sprockets
//= require bootstrap-slider
//= require turbolinks
//= require_tree .
任何意見將不勝感激!我嘗試了很多不同的方法來使複選框起作用,包括使用collection_check_box,但似乎沒有任何工作。
我已經說過:foo,dietaryviolation for:Foo.all,and:diet for:id and:name,因爲沒有:id或:name列(只有飲食列,實際上是名字,以及違反飲食限制的特定成分的成分列),但它仍然沒有出現。 – tintin
':id'肯定存在。軌道中的所有表格都會自動具有「id」列。請嘗試添加它並查看它是否有效。 – angkiki