请你帮我一下。
我有三种型号: 天,课程,菜
// And use it like this
Navigator.push(
context,
CupertinoRoute(
exitPage: HomeScreen(),
enterPage: SecondScreen()));
class Day < ApplicationRecord
has_many :courses
has_many :dishes, through: :courses
end
class Course < ApplicationRecord
belongs_to :day
belongs_to :dish
end
class Dish < ApplicationRecord
has_many :courses
has_many :days, through: :courses
end
查看index.html.erb:
class DaysController < ApplicationController
before_action :require_user
def index
@days = Day.all
end
def show
@day = Day.find(params[:id])
@dishes = @day.dishes
end
end
查看show.html.erb:
<div class="days">
<div class="container">
<% @days.each do |day| %>
<button>
<%=day.weekday %>
<%= link_to "Show menu", day_path(day) %>
</button>
<% end %>
<%= link_to "Back to Profile", profile_dir_path(@user), method: "get" %>
</div>
</div>
我真正需要什么: 我需要通过单击“天”按钮在索引页面上显示菜肴清单。 (当用户单击“工作日”按钮时,他需要查看项目的菜单列表(在我的实现菜中))
但是,正如您所看到的,我在展示页面上做了展示菜。...您能给我一些建议,我如何实现呢?
答案 0 :(得分:1)
您可以尝试使用AJAX请求。在这里,我添加了一个带有日按钮的AJAX请求。
# app/views/days/show.js.erb
$('.days').find('#menuList').html('<%= j (render "dishes", dishes: @dishes) %>');
在这里,我为JS请求添加了一个视图文件。
_dishes.html.erb
这里我要添加# app/views/days/_dishes.html.erb
<h2>Menu</h2>
<%= link_to "Close", "#", id: "closeLink" %>
<% dishes.each do |dish| %>
<div class="dish">
<h3 class="dish-name"><%= dish.name %></h3>
<h3 class="dish-description"><%= dish.description %>
</h3>
</div>
<% end %>
<script>
$('#closeLink').click(function(e) {
e.preventDefault();
$(this).parent().html("");
});
</script>
部分用于渲染菜肴。
{{1}}
享受:-)