Rails 子集选择始终返回 true
我正在尝试在数据集中搜索值。如果数据集包含该值,则应用程序会执行某些操作。如果没有,应用程序会执行其他操作。
数据分解如下:我有附属机构和用户,每个都与另一个具有 HABTM 关系。我有一个页面,用户可以在其中注册附属机构,这些附属机构显示为一组复选框。
我希望选中用户当前注册的所有联营公司的复选框。
这是视图的代码(在 HAML 中)
- @affiliates.each do |a|
%li
%label{ :for => "affiliate_#{a.id}"}= a.name
- if @current_user.affiliates.select{ |ua| ua.id == a.id }
= check_box_tag "affiliate_list[#{a.id}]", 1, true, {:id => "affiliate_#{a.id}"}
- else
= check_box_tag "affiliate_list[#{a.id}]", 1, false, {:id => "affiliate_#{a.id}"}
该代码始终返回 true,因此即使用户尚未注册附属机构,也会选中复选框。
我查找了 .select 方法,但我不断想出表单助手的东西。
I am trying to search a dataset for a value. If the dataset contains the value, the app does something. If it does not, the app does something else.
The data breaks down like this: I have affiliates and users, each with a HABTM relationship to the other. I have a page where the user can sign up for affiliates, which are displayed as a group of checkboxes.
I want the checkboxes for all the affiliates a user has currently signed up for checked.
Here's the code for the view (in HAML)
- @affiliates.each do |a|
%li
%label{ :for => "affiliate_#{a.id}"}= a.name
- if @current_user.affiliates.select{ |ua| ua.id == a.id }
= check_box_tag "affiliate_list[#{a.id}]", 1, true, {:id => "affiliate_#{a.id}"}
- else
= check_box_tag "affiliate_list[#{a.id}]", 1, false, {:id => "affiliate_#{a.id}"}
This code always returning true, and thus, checks boxes, even if a user has not signed up for an affiliate.
I looked up the .select method, but I keep coming up with the form helper stuff.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)