Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Rails Form Issues

Hey all. So i am creating a rails project...

i want to be able to create a resident through a site so that when i create the resident in a form the site id auto populates without end user input (as they wont know what the site id is.)

my sites_controller i have added the following :

def site_resident @site.resident.create(resident_params) end

and

private

def resident_params
  params.require(:resident).permit(:site_id, :unit_number, :f_name, :m_name, :l_name, :dob)
end

i am lined to the page but when i add the form it errors out i have also added this before action to the sites_controller.rb

before_action :set_site, only: [:show, :edit, :update, :destroy] before_action :site_resident, only: [:show, :edit, :update, :destroy]

this is what the form looks like

<%= form_for(@site.residents) do |f| %> --> This is what errors out i just dont know what to do here. <% if @site.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@site.errors.count, "error") %> prohibited this site from being saved:</h2>

  <ul>
  <% @site.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>

and yes the relationships are good!

3 Answers

I think you need to pluralize the residents here if site has many residents: @site.residents.create

even pluralized it still error out with

undefined method `residents' for nil:NilClass

<%= form_for(@site.residents.create) do |f| %> <% if @resident.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@resident.errors.count, "error") %> prohibited this resident from being saved:</h2>

  <ul>

i have also tried <%= form_for(@site.residents) do |f| %>

You haven't provided the form in your question?