Pracede
Pracede

Reputation: 4361

Rails : How to correct undefined method error on form?

I have a form with checkbox.Here is the form :

<div class="border-form-div">
<%= form_for(@user, class: "form-horizontal")  do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user.errors.each do |attribute,message| %> 

        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>



  <% if @user.role == "candidat" %>


    <%= f.fields_for :profileable do |pf| %>

      <div class="field">
        <%= f.label :actif %> <br />
        <%= f.check_box :actif, {}, true, false %> <br />
     </div>

  <% end %>

  <br>

  <div class="actions">
    <%= f.submit "Mettre à jour",class: 'btn btn-primary' %>
  </div>

<% end %>
</div>

I want to go to edit page: http://localhost:3000/users/3/edit But i have the following error :

undefined method `actif' for #<User:0x007fadc2674c48>
Extracted source (around line #35):


      <div class="field">
        <%= f.label :actif %> <br />
        <%= f.check_box :actif, {}, true, false %> <br />
     </div>

It seems that it does not recognize actif as attribute. What the problem. Actif is nw field added in my model and form.

Upvotes: 0

Views: 334

Answers (1)

Ben Hawker
Ben Hawker

Reputation: 949

Change f.checkbox to pf.checkbox I think.

<%= f.fields_for :profileable do |pf| %>

  <div class="field">
    <%= pf.label :actif %> <br />
    <%= pf.check_box :actif, {}, true, false %> <br />
 </div>

Upvotes: 1

Related Questions