1

Models relevant to this question: user, friend, interest, person_interest. Person_interest is polymoprhic and can store interests that either belong to a user or a friend.

Interests is pre-populated with approx. 30 interests. When a user registers they are asked to select their own interests, then lower in the form enter in their friends names select the interests of that person.

Trying to achieve this with <%= friend_f.input :interests, :as => :check_boxes, :label => false %>

but getting the following error:

NoMethodError in User_steps#show

Showing /Users/nelsonkeating/Desktop/ReminDeal/app/views/user_steps/show.html.erb where line #30 raised:

undefined method `to_i' for []:ActiveRecord::Relation
Extracted source (around line #30):

27:         :end_year => Date.today.year - 12,
28:         :order => [:month, :day, :year ] %>
29:       <%= friend_f.input :gender, :collection => ['male','female'] %>
30:       <%= friend_f.input :interests, :as => :check_boxes, :label => false %>
31:     <%end%>
32:   
33: 
Rails.root: /Users/nelsonkeating/Desktop/ReminDeal

Application Trace | Framework Trace | Full Trace
app/views/user_steps/show.html.erb:30:in `block (2 levels) in _app_views_user_steps_show_html_erb__1202886937753978667_70281885932700'
app/views/user_steps/show.html.erb:23:in `block in _app_views_user_steps_show_html_erb__1202886937753978667_70281885932700'
app/views/user_steps/show.html.erb:1:in `_app_views_user_steps_show_html_erb__1202886937753978667_70281885932700'
Request

Parameters:

{"id"=>"show"}

And here's the form..

<%= simple_form_for @user do |f| %>

  <%= f.input :name %>
  <%= f.input :city %>
  <%= f.input :address, :live => true  %>
  <%= f.input :zipcode %>
  <%= f.input :date_of_birth, :as => :date, :start_year => 1900,
    :end_year => Date.today.year - 12,
    :order => [ :day, :month, :year] %>
  <%= f.input :gender, :collection => [:male, :female] %>


    <h4>Select your top 3 interests..</h4>
      <%= f.association :interests, :as => :check_boxes, :label => false %>

    <h4>What holidays do you celebrate?</h4>
      <%= f.association :holidays, :as => :check_boxes, :label => false %> 



<h4>Add up to 10 friends birthdays that you would like to remember..</h4>
    <%= f.simple_fields_for :friends do |friend_f| %>

      <%= friend_f.input :name %>
      <%= friend_f.input :dob, :label => :Birthday, :as => :date, :start_year => Date.today.year - 90,
        :end_year => Date.today.year - 12,
        :order => [:month, :day, :year ] %>
      <%= friend_f.input :gender, :collection => ['male','female'] %>
      <%= friend_f.input :interests, :as => :check_boxes, :label => false %>
     <%end%>


 <%= f.button :submit, :class => 'btn btn-success' %>
      <%end%>

Git repo is here: https://github.com/nelsonkeating/ReminDeal

1
  • So its this line " <%= friend_f.input :interests, :as => :check_boxes, :label => false %>"...
    – js111
    May 19, 2012 at 1:43

2 Answers 2

6
+50

Do you have any reasons to avoid friend_f.association ?

<%= friend_f.association :interests, :as => :check_boxes, :label => false %>

Works for me. Or change :interests to :interest_ids in kasper's code, also should work.

<%= friend_f.input :interest_ids, :as => :check_boxes, :label => false, :collection => Interest.all %>
0
0

Try to write code <%= friend_f.input :interests, :as => :check_boxes, :label => false %> with :collection key. For example:

<%= friend_f.input :interests, :as => :check_boxes, :label => false, :collection => Interest.all %>
5
  • that gives me the following error: "NameError in User_steps#show .....uninitialized constant ActionView::CompiledTemplates::Interests"
    – js111
    May 22, 2012 at 18:37
  • 1
    In kasper's code, change :collection => Interests.all to :collection => Interest.all and try that. The model should be singular.
    – Steph Rose
    May 22, 2012 at 19:00
  • I do it all the time. Hope that helps! If that doesn't solve the problem, I may have another solution.
    – Steph Rose
    May 22, 2012 at 20:59
  • Thanks for the answer!.. now im getting "undefined method `to_i' for []:ActiveRecord::Relation"
    – js111
    May 22, 2012 at 23:40
  • @StephRose what was your alt. idea?? Thanks again.
    – js111
    May 23, 2012 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.