开发者

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开发者_C百科'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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜