Question

User model - built with devise.

class User < ActiveRecord::Base
// some stuff 
has_one :expert
accepts_nested_attributes_for :expert, :update_only => true
// more stuff

Expert model - built withOUT devise.

class Expert < ActiveRecord::Base  
belongs_to :user

User controller:

def edit
  @user = User.friendly.find(params[:id])
  @title = "Edit Profile"
end

Expert controller:

def edit
  @expert = Expert.find(params[:id])
  @title = "Edit Expert Profile"
end

Edit user view form partial:

<%= form_for(@user) 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.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :Name %><br>
    <%= f.text_field :name, :class => "form-control" %>
  </div>


  <div class="field">
    <%= f.label :Email %><br>
    <%= f.text_field :email, :class => "form-control" %>
  </div>

  <div class="field">
    <%= f.label :Phone %><br>
    <%= f.text_field :phone, :class => "form-control" %>
  </div>


  <div class="field">
    <%= f.label :Password %><br>
    <%= f.text_field :password, :class => "form-control" %>
  </div>

  <div class="field">
    <% f.fields_for :expert do |e| %>
      <%= e.label :Location %><br />
      <%= e.text_field :Location %>
    <% end %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

The part of the form which is supposed to display the :Location field for the expert model does not display on the view.. screenshot here: http://i.imgur.com/JifVuv7.png

Can't figure out why this is happening, any help would be appreciated!

Was it helpful?

Solution

As per ERB syntax, you need to use:

<%= %> to execute the enclosed ruby code + render results <% %> to execute the enclosed ruby code

You used <% %> for f.fields_for which is why the nested fields were not rendered.

Change the code as below:

<%= f.fields_for :expert do |e| %>
  <%= e.label :Location %><br />
  <%= e.text_field :Location %>
<% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top