我有三个模型,用户模型,餐厅模型和visit_restaurant模型。
class Restaurant < ActiveRecord::Base
has_many :visited_restaurants, dependent: :destroy
has_many :users, through: :visited_restaurants
class User < ActiveRecord::Base
has_many :visited_restaurants, dependent: :destroy
has_many :restaurant, through: :visited_restaurants
class VisitedRestaurant < ActiveRecord::Base
belongs_to :user
belongs_to :restaurant
我有两个索引页面,一个用于餐馆,一个用于visited_restaurants。餐厅索引页面将显示您所在地区的所有餐厅,不包括您访问过的餐馆。 visited_restaurant索引页面只显示您访问过的餐馆。
class VisitedRestaurantsController < ApplicationController
def index
params[:order_by].nil? ? @sort_by = "type_of_cuisine asc" : @sort_by = sort_col + " " + sort_dir
@visited_restaurants = current_user.restaurants.order(@sort_by)
respond_to do |format|
format.html
format.js
end
end
private
def sort_col
@sort_options = ["type_of_cuisine", "rating", "cost"]
unless params[:order_by].nil?
@sort_options.include?(params[:order_by].split(" ")[0]) ? params[:order_by].split(" ")[0] : "type_of_cuisine"
end
end
def sort_dir
unless params[:order_by].nil?
%w[asc desc].include?(params[:order_by].split(" ")[1]) ? params[:order_by].split(" ")[1] : "asc"
end
end
class RestaurantsController < ApplicationController
def index
params[:order_by].nil? ? @sort_by = "type_of_cuisine asc" : @sort_by = sort_col + " " + sort_dir
@restaurants = Restaurant.where.not(id: current_user.restaurant_ids).order(@sort_by)
respond_to do |format|
format.html
format.js
end
end
private
def sort_col
@sort_options = ["type_of_cuisine", "rating", "cost"]
unless params[:order_by].nil?
@sort_options.include?(params[:order_by].split(" ")[0]) ? params[:order_by].split(" ")[0] : "type_of_cuisine"
end
end
def sort_dir
unless params[:order_by].nil?
%w[asc desc].include?(params[:order_by].split(" ")[1]) ? params[:order_by].split(" ")[1] : "asc"
end
end
观点
views/shared/_order_by.html.erb
<span>Sort by:</span>
<%= form_tag(params, method: :get, remote: true) do %>
<%= select_tag(:order_by, options_for_select(order_by_options, params[:order_by])) %>
<% end %>
views/visited_restaurants/index.html.erb
<%= render 'shared/order_by' %>
<div id="visited-restaurants-container">
<%= render 'visited_restaurants_list' %>
</div>
views/restaurants/index.html.erb
<%= render 'shared/order_by' %>
<div id="restaurants-container">
<%= render 'restaurants_list' %>
</div>
views/visited_restaurants/index.js.erb
$("#visited-restaurants-container").html("<%= escape_javascript(render('visited_restaurants/visited_restaurants_list')) %>");
views/restaurants/index.js.erb
$("#restaurants-container").html("<%= escape_javascript(render('restaurants/restaurants_list')) %>");
在restaurants / index.html.erb页面上的排序有效,但是当我点击visited_restaurants / index.html.erb页面上的下拉列表时,没有任何反应。查看网络标签下的Chrome开发工具,当我点击visited_restaurants页面上的下拉菜单时,它会调用views / restaurants中的index.js.erb文件,而不是位于views / visited_restaurants中的文件。为什么会发生这种情况,我需要做些什么才能调用正确的index.js.erb文件?
编辑:
两个views / restaurants / restaurants_list.html.erb和views / visited_restaurants / visited_restaurants_list.html.erb都是相同的,除了visited_restaurants_list引用for循环中的@visited_restaurants实例变量。我最初也把它作为一个共享部分,但我在尝试解决这个问题时改变了这一点。一旦修复,我将再次成为共享部分。
views/restaurants/restaurants_list.html.erb
<% for restaurant in @restaurants %>
<div class="restaurant-list-container">
<p>Name:</p>
<p><%= restaurant.name %></p>
<p>Rating:</p>
<p><%= restaurant.rating %></p>
<p>Category:</p>
<p><%= restaurant.type_of_cuisine %></p>
<p>Cost:</p>
<p><%= restaurant.cost %></p>
<p>Address:</p>
<p><%= restaurant.address %></p>
</div>
<% end %>
编辑2:
Form on the restaurants page:
<form accept-charset="UTF-8" action="/restaurants" data-remote="true" method="get">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓">
</div>
<select id="order_by" name="order_by">
<option value="type_of_cuisine asc">Category - asc</option>
<option value="type_of_cuisine desc">Category - desc</option>
<option value="rating asc">Rating - asc</option>
<option value="rating desc">Rating - desc</option>
<option value="cost asc">Cost - asc</option>
<option value="cost desc">Cost - desc</option>
</form>
Form on the visited_restaurants page:
<form accept-charset="UTF-8" action="/visited_restaurants" data-remote="true" method="get">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓">
</div>
<select id="order_by" name="order_by">
<option value="type_of_cuisine asc">Category - asc</option>
<option value="type_of_cuisine desc">Category - desc</option>
<option value="rating asc">Rating - asc</option>
<option value="rating desc">Rating - desc</option>
<option value="cost asc">Cost - asc</option>
<option value="cost desc">Cost - desc</option>
</form>