Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript rails和mongoid-滚动特定记录组_Javascript_Html_Ruby On Rails_Mongodb - Fatal编程技术网

Javascript rails和mongoid-滚动特定记录组

Javascript rails和mongoid-滚动特定记录组,javascript,html,ruby-on-rails,mongodb,Javascript,Html,Ruby On Rails,Mongodb,我有一个用Rails和mongoDB实现的站点来提交、阅读和更新项目。 网站上有几个不同的区域,您可以查看项目并根据搜索关键字或不同的查询方法对其进行筛选。 例如:在主页上有一个要排序的类别列表用例、状态、级别。当用户单击“用例”按钮时,将显示一个列表,其中包含更具体的用例类别,例如“电子商务”,如果用户单击该按钮,div将填充具有等同于电子商务的用例的项目。从这一点上,他们可以点击一个“详细信息”按钮,该按钮将向他们显示该项目的所有属性。 在这个“详细”视图中,我有一个“下一步”和“上一步”按

我有一个用Rails和mongoDB实现的站点来提交、阅读和更新项目。 网站上有几个不同的区域,您可以查看项目并根据搜索关键字或不同的查询方法对其进行筛选。 例如:在主页上有一个要排序的类别列表用例、状态、级别。当用户单击“用例”按钮时,将显示一个列表,其中包含更具体的用例类别,例如“电子商务”,如果用户单击该按钮,div将填充具有等同于电子商务的用例的项目。从这一点上,他们可以点击一个“详细信息”按钮,该按钮将向他们显示该项目的所有属性。 在这个“详细”视图中,我有一个“下一步”和“上一步”按钮。现在,它通过递增和递减项目ID号来滚动项目数据库。 我希望“下一步”和“上一步”按钮能够滚动浏览它们在填充的div中显示的列表。它可以通过名为@items的实例变量访问,该变量在items控制器中初始化,但即使保存为全局变量,也无法在详细信息视图中看到。。。 我想到的唯一选择是将项目列表作为URL参数传递,但这样做似乎很麻烦。 是否有其他方法在不同视图之间传递实例变量? 示例代码: 项目_controller.rb:

list.html.erb:

show.html.erb-滚动按钮:


使用相同的范围检索项目列表和单个项目及其下一个和上一个项目

 # item_controller.rb     

 def list
   @items = items_scope
 end

def show
  @item = items_scope.find(params[:id])
  @next_item_id = items_scope.select(:id).where("id > ?", params[:id]).first
  @previous_item_id = items_scope.select(:id).where("id < ?", params[:id]).last
end

private

def items_scope
  # this scope might depend on url parameters which, for example, present current chosen category
  # `order("id ASC")` is a very important part. it makes this solution work
  Item.order("id ASC").any_other_scopes_or_conditions_depending_on_chosen_category
end
他认为:

<%= link_to item_path(:id => @next_item_id) ...
<%= link_to item_path(:id => @previous_item_id) ...

对于这种情况,此解决方案将不起作用-我需要能够将@items列表从list.html.erb视图传递到show.html.erb视图
  <div class="panel-footer">
    <%if @item.id != @first_id %>
        <% #need to change to scroll to previous in $list %>
        <%= link_to item_path(:id => (@item.id-1)), class: 'btn btn-default' do %>
            <span class="glyphicon glyphicon-menu-left"></span>&nbsp;Back
        <% end %>
    <% end %>
    <%if @item.id != @last_id %>
        <% #need to change to scroll to next in $list %>
        <%= link_to item_path(:id => (@item.id+1)), class: 'btn btn-default' do %>
            Next&nbsp;<span class="glyphicon glyphicon-menu-right"></span>
        <% end %>
    <% end %>
  </div>
 # item_controller.rb     

 def list
   @items = items_scope
 end

def show
  @item = items_scope.find(params[:id])
  @next_item_id = items_scope.select(:id).where("id > ?", params[:id]).first
  @previous_item_id = items_scope.select(:id).where("id < ?", params[:id]).last
end

private

def items_scope
  # this scope might depend on url parameters which, for example, present current chosen category
  # `order("id ASC")` is a very important part. it makes this solution work
  Item.order("id ASC").any_other_scopes_or_conditions_depending_on_chosen_category
end
<%= link_to item_path(:id => @next_item_id) ...
<%= link_to item_path(:id => @previous_item_id) ...