Ruby on rails 如何在rails中加载页面时自动单击链接
内部控制器Ruby on rails 如何在rails中加载页面时自动单击链接,ruby-on-rails,Ruby On Rails,内部控制器 if params[:name]==params[:name] @categories=Category.where(:hotel_id=>params[:hotel_id]) @menus=Menu.where(:category_id=> params[:id]).sorted @cart = current_cart end 在索引页中: <%@categories.each do |d|%> <%= link_to d.
if params[:name]==params[:name]
@categories=Category.where(:hotel_id=>params[:hotel_id])
@menus=Menu.where(:category_id=> params[:id]).sorted
@cart = current_cart
end
在索引页中:
<%@categories.each do |d|%>
<%= link_to d.name ,{:action=>'index',:id=>d.id,:hotel_id=>d.hotel_id},class: "btn-orange" %>
<%end%>
'索引',:id=>d.id,:hotel\u id=>d.hotel\u id},类别:“btn橙色”%>
我正在从数据库中获取所有类别和受人尊敬的菜单项。如何在页面加载时自动单击第一个类别链接。要直截了当地回答您的问题,您必须: -- 结构
<%@categories.each_with_index do |d,index|%>
<%= link_to d.name ,{:action=>'index',:id=>d.id,:hotel_id=>d.hotel_id},id:first_"#{index}",class: "btn-orange" %>
<%end%>
====================js code to click when page is ready============
$(document).ready(function(){
setTimeout(function() {
//click after 2 seconds when page loads or remove it or use=> $('#first_0').trigger('click'); directly without setTimeout in document.ready()
$("#first_0").click();
}, 2000);
})//document ends
不用说,这并不意味着您将创建一个健壮、高效的系统。相反,这意味着你将调用一个你不需要调用的事件
如注释中所述,控制器似乎是发生此类故障的地方:
#config/routes.rb
resources :hotels do
resources :categories
end
#app/models/hotel.rb
class Hotel < ActiveRecord::Base
has_many :categories
end
#app/models/category.rb
class Category < ActiveRecord::Base
has_many :menus
end
#app/controllers/categories_controller.rb
class CategoriesController < ApplicationController
before_action :set_cart
def index
@hotel = Hotel.find params[:hotel_id]
@categories = @hotel.categories
@menus = @categories.find(params[:id]).menus.sorted
end
private
def set_cart
@cart = current_cart
end
end
#config/routes.rb
资源:酒店有吗
资源:类别
结束
#app/models/hotel.rb
一流酒店
您可以使用触发器
对jquery调用操作,如下所示:
$(documnet).on("page:load ready", function(){
$("a.first_link").trigger("click");
})
您可以阅读更多内容,并且。使用获取/检测第一个元素(这是您想要的),然后使用jquery单击它
查看文件
<%@categories.each_with_index do |d,index|%>
<%= link_to d.name ,{:action=>'index',:id=>d.id,:hotel_id=>d.hotel_id},id:first_"#{index}",class: "btn-orange" %>
<%end%>
====================js code to click when page is ready============
$(document).ready(function(){
setTimeout(function() {
//click after 2 seconds when page loads or remove it or use=> $('#first_0').trigger('click'); directly without setTimeout in document.ready()
$("#first_0").click();
}, 2000);
})//document ends
'索引',:id=>d.id,:hotel_id=>d.hotel_id},id:first_“{index}”,类:“btn橙色”%>
============================页面准备就绪时单击的js代码============
$(文档).ready(函数(){
setTimeout(函数(){
//页面加载或删除时,请在2秒钟后单击,或使用=>$(“#first_0”)。直接触发('click');无需在document.ready()中设置超时
$(“#第一个0”)。单击();
}, 2000);
})//文件结尾
如果参数[:name]==params[:name]
??你为什么不在控制器级别重定向?如果你给我们更多的上下文,我们将更好地提供帮助