如何基于组合框调用控制器操作? [英] How can I call a controller action based on combo box?

查看:148
本文介绍了如何基于组合框调用控制器操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ruby on Rails 3.2.2和ruby 1.9。



我有一个模型投标,其中一个属性是状态。在索引页面上,首次加载时,它会根据已登录的用户公司显示投标数据(也会显示状态)。



我想要一个包含所有状态的组合框过滤器(Initiated,Closed,Won ..etc),所以当用户更改过滤器时,我还希望通过将状态提交给控制器来更改表​​数据,控制器将根据状态过滤出投标。我该怎么办?如何使用Ajax(因为唯一的改变是表数据不是整个页面)?



我也有不同的过滤器选项,而不是状态,例如通过bid_amount(也是属性之一)。如何根据数据结合bid_amount过滤器与状态(即,如果用户想要显示出价金额为b / n 10,000-100,000的关闭状态)。我不会有问题写在控制器上的查询,但我不知道如何使用它从视图。



由于ruby没有枚举像Java我也从模型类中的数组加载这些状态。这是一个很好的方法吗?



任何建议也将不胜感激。

解决方案

通过ajax执行事务的rails方法是:remote => true。
一个简单的搜索会得到很多教程。



非ajax解决方案可能是这样的结构。

  def index 
如果params [:status] || params [:bid_amount]
#Tailored Query
else
#Generic Query
end
end

查看

 <%= form_tag index_route_path do%> 
#通过Model
提取的状态符号化
#出价金额框
<%end%>

#显示查询结果

帮助您使用您的枚举
https://github.com/nofxx/symbolize

它仍然不能清楚我想要做什么。如果您希望根据用户设置状态填充您的出价金额框。您可以触发表单提交onChange,并使用助手填充您的bid_amount值。


I am using Ruby on Rails 3.2.2 and ruby 1.9 .

I do have a model tender and one of the attributes is status. On the index page, the first time it is loaded, it displays the tender data based on the user's company who already logged in (it will also display the status).

I want to have a filter by combo box which will contain all the statuses (Initiated, Closed, Won ..etc )so when the user changes the filter by, I want to change the table data as well by submitting the status to a controller which will filter out tenders based on the status. How can I do that? How about using Ajax (since the only thing that is changed is the table data not the whole page)?

I also have different filter by options other than the status, like by the bid_amount (also one of the attributes). How can I combine the bid_amount filter by data with the status (i.e if the user wants to display closed statuses which has a bid amount b/n 10,000-100,000). I won't have a problem writing the query on the controller but I don't know how to use it from the view.

Since ruby doesn't have an Enum like Java I also load those statuses from an array in the model class. Is that a good way of doing it?

Any recommendations will also be appreciated.

解决方案

The rails way of executing things via ajax is using :remote => true. A simple search will get you quite a few tutorials.

Non ajax solution might be structured like this.

 def index
    if params[:status] || params[:bid_amount]
         # Tailored Query
    else
         # Generic Query
    end
 end

View

 <%= form_tag index_route_path do %>
     # Statuses that are pulled form symbolize via Model
     # Bid Amount Boxes
 <% end %>

 # Display query results 

ALso I would checkout the gem Symbolize, to help you out with your Enums https://github.com/nofxx/symbolize

Its still not super clear to me what our looking to do. If your looking to populate your bid amount boxes based on what the user sets the status too. You could trigger a form submit onChange, and use a helper to populate your bid_amount values.

这篇关于如何基于组合框调用控制器操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆