How can I call a controller action based on combo box?

拟墨画扇 提交于 2019-12-25 05:21:36

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/11279655/how-can-i-call-a-controller-action-based-on-combo-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!