Ruby on Rails:重新排序checkbox标签
我有一个应用程序,用户可以将项目输入数据库。其中一个字段允许用户选择多种技术。我想要技术列表按字母顺序显示,因为目前它们按照他们输入数据库的顺序出现。
I have an app where a user can enter projects into a database. One of the fields allows the user to pick multiple technologies. I want the list of technologies to appear in alphabetical order, because at the moment, they appear in the order they were entered into the database.
这是我的新操作我的专案控制器:
Here is my new action in my project controller:
def new
@project = Project.new
@technol = Technol.new(params[:tech])
@all_technols = Technol.all
tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil?
@project_technol = @project.projecttechnols.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @project }
end
end
这是我的新视图的一部分:
Here is part of my new view:
<div class="tech" STYLE="text-align: left;">
<b>Technologies:</b>
<style>
.split { text-align:left; }
</style>
<p><ul>
<% for technol in Technol.all %>
<li class="split">
<%= check_box_tag "project[technol_ids][]", technol.id, @project.technols.include?(technol) %>
<%= technol.tech %>
</li>
<% end %>
</ul>
</p>
有没有任何想法?我是新的rails,所以请记住这一点,当试图回答。提前感谢。
Does anyone have any ideas? I am new to rails so please remember this when trying to answer. Thanks in advance.
您应该订购您的技术列表:
You should order the list of your thechnologies:
@all_technols = Technol.order('tech ASC') # in the controller's action
然后在视图中使用此变量,而不是调用 Technol.all
第二次:
And then use this variable in the view instead of calling Technol.all
second time:
<% @all_technols.each do |technol| %>
your code without changes here
<% end %>