简体   繁体   中英

Rails subset select always returns true

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.

if @current_user.affiliates.include?(a)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM