【发布时间】:2015-01-28 16:53:38
【问题描述】:
我正在尝试根据选中的复选框显示文本。
我有 3 个状态,所以当我选中一个或两个复选框时,我会显示在没有 javascript 的情况下选择的选项。
例如:
if I check1 will show me the text active.
if I check2 will show me the text inactive.
if I check3 will show me the text inactive and active.
if I check1 and check 2 will show me the text "active","inactive".
if I check1 and check 2 will show me the text "active","removed".
if I check1 and check 2 will show me the text "removed","inactive".
只需根据选中的复选框保留文本即可。
这里是控制器:
@search_state = params[:search_state]
@people = Person.all
这里是视图:
<%= form_tag(people_path,:method => "get") do %>
#### BEGIN CHECKBOX #####
<% if @search_state.blank? %>
Check1 <%= check_box_tag "search_state[]", "0" %>
Check2 <%= check_box_tag "search_state[]", "1" %>
Check3 <%= check_box_tag "search_state[]", "2" %>
<% else %>
Check1 <%= check_box_tag "search_state[]", "0", @search_state.collect(&:to_i).include?(0) %>
Check2 <%= check_box_tag "search_state[]", "1", @search_state.collect(&:to_i).include?(1) %>
Check3 <%= check_box_tag "search_state[]", "2", @search_state.collect(&:to_i).include?(2) %>
<% end %>
#### END CHECKBOX ####
### WANT TO SHOW A TEXT ACCORDING CHECKBOXES SELECTED ###
<% if @search_state.to_s=="0"%>
Active
<% elsif @search_state.to_s=="1"%>
Inactive
<% elsif @search_state.to_s=="2" %>
Removed
<% elsif @search_state.to_s==["0","1"] %>
Active,Inactive
<% elsif @search_state.to_s==["0","2"] %>
Active,Removed
<% elsif @search_state.to_s==["1","2"] %>
Inactive,Removed
<% elsif @search_state.to_s==["0","1","2"] %>
Active,Inactive,Removed
<% end %>
### END WANTTO SHOW A TEXT ACCORDING CHECKBOXES SELECTED ###
<% end %>
这里是文件和演示:
http://runnable.com/VMkReiHVr0FbfQKN/checkbox-project-test-for-ruby-on-rails
http://runnable.com/VMkRe1xPNqFWSMtd/output
我的代码太长,我认为这不是正确的做法。
例如在这种情况下我有 3 个状态,如果我有更多的状态会太长和混乱。
有人可以帮我在没有几行代码的情况下使用 ruby on rails 制作复选框吗?
我知道使用 Javascript 是解决方案,但这是解决问题的唯一方法吗?请问如何解决?
我将非常感谢所有帮助。
【问题讨论】:
-
你能解释一下你想要达到的目标吗?我不认为你正在以正确的方式做“它”,但我不确定“它”是什么......你能写出功能的预期行为吗?
-
Sorry MrYoshiji ,我的意思是没有根据复选框编写几行代码,如果我有更多的复选框,代码会太长,我认为不是正确的方法需要使它更动态
标签: javascript ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4