我有一个应用程序,用户可以在其中将项目输入数据库。其中一个字段允许用户选择多种技术。我希望技术列表按字母顺序显示,因为目前它们按输入数据库的顺序显示。
这是我在项目 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
这是我的新观点的一部分:
<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 的新手,所以在尝试回答时请记住这一点。提前致谢。
最佳答案
您应该订购您的技术列表:
@all_technols = Technol.order('tech ASC') # in the controller's action
然后在 View 中使用这个变量而不是第二次调用 Technol.all
:
<% @all_technols.each do |technol| %>
your code without changes here
<% end %>
关于ruby-on-rails - ruby rails : re-order checkbox tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13269961/