0

I have the following models:

class Person < ApplicationRecord
  has_many :interests, dependent: :destroy
  accepts_nested_attributes_for :interests

  validates_presence_of  :email
  validates_inclusion_of :gender, :in => %w(M F), message: "Gender can     only be in M or F"
  has_secure_password

  def name
    "#{first_name} #{last_name}"
  end

  def interests_concatenated
    interests.map { |i| i.interest }.join(", ")
  end
end

class Interest < ApplicationRecord
  belongs_to :person
end

My controller is as follows:

class PeopleController < ApplicationController

def index
  @person = Person.all
end

def new
  @person = Person.new
  @person.interests.build
end

def create
  @person = Person.new(people_params)
  if @person.save
    session[:user_id] = @person.id
    redirect_to(people_path)
  else
    flash = "Email or gender can't be blank!"
    render 'new'
  end
end

private
  def people_params
    params.require(:person).permit(:email, :first_name, :last_name, :gender, :password,:password_confirmation, interests_attributes: [:hobby])
  end
end

My form is as follows:

<%= form_for @person  do |f| %>
<p>
  <%= f.label :email %> <br>
  <%= f.text_field :email %>
</p>
<p>
  <%= f.label :first_name %> <br>
  <%= f.text_field :first_name %>
</p>
<p>
  <%= f.label :last_name %> <br>
  <%= f.text_field :last_name %>
</p>
<p>
  <%= f.label :gender %> <br>
  <%= f.label(:gender_male, "Male") %>
  <%= f.radio_button(:gender, "M") %> <br>
  <%= f.label(:gender_female, "Female") %>
  <%= f.radio_button(:gender, "F") %> <br>
</p>
<p>
  <%= f.label :password %> <br>
  <%= f.password_field :password %>
</p>
<p>
  <%= f.label :password_confirmation %> <br>
  <%= f.password_field :password_confirmation %>
</p>
<p>
  <%= f.fields_for :interests do |i| %>
    <%= i.label :hobby %>
    <%= i.text_field :hobby  %>
  <% end %>
</p>
<p>
  <%= f.submit %>
</p>
<% end %>

Here is the byebug console log when I run it:

console error message

Very stumped why it's not working. Could it be something to do with the parameters?

Here is the log file when I submit the form:

enter image description here

MLZ
  • 403
  • 7
  • 20
  • have you tried `interests_attributes: [:hobby]` ? – Alexandre Angelim Jan 19 '17 at 22:32
  • Yes, actually it was interests_attributes: [:hobby] in the code, I don't know why it shows up at interests_attributes[:hobby] when I posted here – MLZ Jan 19 '17 at 22:37
  • Instead of `@interests = @person.interests.new` try `@interests = @person.interests.build` ? – Taryn East Jan 19 '17 at 22:40
  • I just tried @person.interests.build and got the same error – MLZ Jan 19 '17 at 22:43
  • Hmm, that's odd... Try adding `:person_id` to the `interests_attributes` permit/require line ? (note: shouldn't need to afaik) – Taryn East Jan 19 '17 at 23:11
  • @TarynEast I just tried that as well but still didn't work – MLZ Jan 19 '17 at 23:14
  • :/ weird... can you show us the relevant lines from the console output/logfiles after submitting? usually it'll say something like "Unpermitted parameters: blah blah blah" and sometimes the params that come through are really useful too... eg they show parameter-nesting errors that you otherwise miss... – Taryn East Jan 19 '17 at 23:19
  • @TarynEast I posted a screenshot of the byebug debugger, does that help? – MLZ Jan 19 '17 at 23:33
  • Nah... the logs would really help us out a bit more here :) You'll get them from either the terminal/console window, or from `logs/development.log` - just scroll to the end and then watch what gets output after you click "submit" (remove the `pry` for the short time while you're doing it) – Taryn East Jan 20 '17 at 01:36
  • @TarynEast ok, I just posted the development.log entry – MLZ Jan 20 '17 at 23:56
  • Ok... and a couple of lines after that too, would be helpful - anything that occurs after you click the submit button. Also if you can - please post it as text, rather than an image, in case we need to copy/paste stuff from it :) That said - at first glance it looks like ti's coming through ok... but we can't tell for sure until we see what it says when it tries to actually save the `person` object. Did you remove the `binding.pry` before running this (and getting the details from the log)? – Taryn East Jan 22 '17 at 21:19
  • Hi Taryn, that was actually all of the log that was printed. Sorry, I had to make it into an image file because when I tried to copy&paste the code, StackOverflow wasn't allowing me to post because it said my submission was" almost entirely code". I had removed binding.pry earlier. But I found a workaround solution down below. Thanks for your suggestions! – MLZ Jan 23 '17 at 07:13

2 Answers2

2

Instead of:

@interests = @person.interests.new

try

@interests = @person.interests.build

new creates a fresh, clean, completely empty new object... but build is the special Rails association method that will fill it with appropriate defaults (like, eg the right person_id)

Taryn East
  • 27,486
  • 9
  • 86
  • 108
0

I found a working solution by adding this in my interests model:

class Interest < ApplicationRecord
  belongs_to :person, **optional: true**
end

Since @person fails to save each time, the biggest clue was in the error message "Interest person must exist", I found this StackOverflow solution to be helpful. Also this blog post on why this is needed was helpful in shedding light on the issue.

Thanks to everyone that weighed in on it!

Community
  • 1
  • 1
MLZ
  • 403
  • 7
  • 20