Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulating Checkboxes on Rails from DB
    primarykey
    data
    text
    <p>I am just starting to learn about Rails (ver. 3), and I am trying to create a user registration form with checkbox options for their interests, which may be 0 or more selections. I generated a User scaffold, and an interest model. I seeded the interests table in Postgresql with data:</p> <pre><code> Table "public.interests" Column | Type | Modifiers ------------+--------------------+------------------------------------------------ id | integer | not null default nextval('interests_id_seq'::regclass) interest | character varying(40) | created_at | timestamp without time zone | updated_at | timestamp without time zone | Indexes: "interests_pkey" PRIMARY KEY, btree (id) </code></pre> <p>The view has:</p> <pre><code>&lt;div class="form_row"&gt; &lt;label for="interest_ids[]"&gt;Interests:&lt;/label&gt; &lt;% for interest in Interest.find(:all) do %&gt; &lt;br&gt;&lt;%= check_box_tag 'interest_ids[]', interest.id, @model.interest_ids.include?(interest.id) %&gt; &lt;%= interest.name.humanize %&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> <p>(based on <a href="https://stackoverflow.com/questions/621340/checkboxes-on-rails">Checkboxes on Rails</a>)</p> <p>The model interest.rb has:</p> <pre><code>class Interest &lt; ActiveRecord::Base has_and_belongs_to_many :users end </code></pre> <p>The users controller users_controller.rb has:</p> <pre><code> def new @user = User.new respond_to do |format| format.html # new.html.erb format.xml { render :xml =&gt; @user } end end </code></pre> <p>When I view the page I get:</p> <pre><code>undefined method `interest_ids' for nil:NilClass </code></pre> <p>Can someone tell me what's wrong? Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload