简体   繁体   English

Rails 4-如何在一个视图中保存来自不同下拉菜单的选择并将其传递给控制器​​以接收相应的数据

[英]Rails 4 - How to save selections from different dropdown menus in one view and passing them to controller to receive corresponding data

I'm very new to Rails and have just started to create my first app. 我是Rails的新手,刚刚开始创建我的第一个应用程序。 this question may sound a little weird. 这个问题听起来有点奇怪。

So i have two drop down menus, Year and Region, they each have some selections. 所以我有两个下拉菜单,年份和地区,它们每个都有一些选择。 i have an articles controller and model to store some info. 我有一个articles控制器和模型来存储一些信息。 I'm trying to make this action work: 我正在尝试使此操作有效:

first make a selection from "Year", say 2014. As soon as i click on 2014 i want to display all the articles from the database with the year 2014 首先从“ Year”中进行选择,例如2014年。我单击2014年后,我想显示2014年数据库中的所有articles

then, while the "Year" dropdown menu is still in 2014, I make a selection in the "Region" dropdown menu, say US, and have the view display articles that belongs to 2014 and US. 然后,虽然“年份”(Year)下拉菜单仍在2014年,但我在“地区”(Region)下拉菜单中进行了选择,例如US,并在视图中显示了属于2014和US的文章。

is it possible to do all the above without using ajax? 是否可以在不使用ajax的情况下完成以上所有操作?

here's my view code for articles/index 这是我的articles/index查看代码

<!-- Region -->
<div class="dropdown">
  <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
    Region
    <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
    <li><%= link_to "All Regions", ?????_article_path(???) %></li>
    <% @articles.each do |article| %>
        <li>
            <%= link_to article.region, ?????_article_path(???)%>
        </li>
    <%end%>
  </ul>
</div>

<!-- Years -->
<div class="dropdown">
  <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
    Year
    <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
    <li><%= link_to "All Years", ?????_article_path(???) %></li>
    <% @articles.each do |article| %>
        <li>
            <%= link_to article.year, ?????_article_path(???)%>
        </li>
    <%end%>
  </ul>
</div>

here's my ArticlesController , it just have one index method 这是我的ArticlesController ,它只有一个index方法

class ArticlesController < ApplicationController 类ArticlesController <ApplicationController

def index
    @articles = Article.all
end

So i thought about putting another method in there called selection but i'm not sure how to pass the second dropdown menu's selection. 因此,我考虑过将另一种方法称为selection但是我不确定如何通过第二个下拉菜单的选择。

If you want to actively get the articles from the database, you need AJAX. 如果要主动从数据库中获取文章,则需要AJAX。 In case you are OK with loading the articles together with the view, you can simply store them as a pre-fetched JSON object like this: 如果可以将文章和视图一起加载,可以将它们存储为预提取的JSON对象,如下所示:

<script>
   var articles = <%= raw @articles.to_json %>;alert(articles);
</script>

And then either manually go through this JS object. 然后手动通过此JS对象。

Tip: to have more control over displaying and rendering of JS objects, install backbone.js ( http://backbonejs.org/ ), install backbone.js query ( https://github.com/davidgtonge/backbone_query ) make a articles Backbone model (tutorials on backbone.js page are really simple and helpful), articles_collection Backbone collection, query it the way you want, and then make HTML code via Javascript and paste it on the page. 提示:要对JS对象的显示和呈现有更多控制,请安装eleteger.js( http://backbonejs.org/ ),然后安装boost.js查询( https://github.com/davidgtonge/backbone_query )撰写文章骨干模型(骨架.js页面上的教程非常简单且实用),articles_collection骨干集合,以您想要的方式对其进行查询,然后通过Javascript编写HTML代码并将其粘贴到页面上。 I prefer using Backbone.js underscore.js templates for that, they are an excellent tool to master. 我更喜欢使用Backbone.js underscore.js模板,它们是一个很好的掌握工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM