Skip to content

Instantly share code, notes, and snippets.

@crova
Created April 5, 2018 19:40
Show Gist options
  • Save crova/3976596b2ea5d9ac96ab38cf9e1cddc0 to your computer and use it in GitHub Desktop.
Save crova/3976596b2ea5d9ac96ab38cf9e1cddc0 to your computer and use it in GitHub Desktop.
Getting undefined method 'map' for Nil Class (coming from Kind.all on the select_options method)
<%= form_for(@contact, html: { class: "basic-grey" }) do |f| %>
<% if @contact.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@contact.errors.count, "error") %> prohibited this contact from being saved:</h2>
<ul>
<% @contact.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<h1>Contacts</h1>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :kind_id %><br>
<%= collection_select(:contact, :kind_id, @kind_options_for_select, :id, :description) %>
</div>
<%= f.fields_for :address do |address_fields| %>
<div class="field">
<%= f.label :street %><br>
<%= address_fields.text_field :street %>
</div>
<div class="field">
<%= f.label :city %><br>
<%= address_fields.text_field :city %>
</div>
<div class="field">
<%= f.label :state %><br>
<%= address_fields.text_field :state %>
</div>
<% end %>
<div class="field">
<%= f.label :rmk %><br>
<%= f.text_area :rmk %>
</div>
<div class="field">
<%= f.submit "Enviar", class: "button" %>
<%= link_to 'Back', contacts_path, class: "button"%>
</div>
<% end %>
class Contact < ApplicationRecord
belongs_to :kind
has_one :address
accepts_nested_attributes_for :address
end
class ContactsController < ApplicationController
before_action :set_contact, only: [:show, :edit, :update, :destroy]
# GET /contacts
# GET /contacts.json
def index
@contacts = Contact.all
@meu_nome = "Crova"
end
# GET /contacts/1
# GET /contacts/1.json
def show
end
# GET /contacts/new
def new
@contact = Contact.new
@contact.build_address
options_for_select
end
# GET /contacts/1/edit
def edit
options_for_select
end
# POST /contacts
# POST /contacts.json
def create
@contact = Contact.new(contact_params)
@contact.build_address
respond_to do |format|
if @contact.save
format.html { redirect_to @contact, notice: 'Contact was successfully created.' }
format.json { render :show, status: :created, location: @contact }
else
format.html { render :new }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /contacts/1
# PATCH/PUT /contacts/1.json
def update
respond_to do |format|
if @contact.update(contact_params)
format.html { redirect_to @contact, notice: 'Contact was successfully updated.' }
format.json { render :show, status: :ok, location: @contact }
else
format.html { render :edit }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end
# DELETE /contacts/1
# DELETE /contacts/1.json
def destroy
@contact.destroy
respond_to do |format|
format.html { redirect_to contacts_url, notice: 'Contact was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def options_for_select
@kind_options_for_select = Kind.all
end
# Use callbacks to share common setup or constraints between actions.
def set_contact
@contact = Contact.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def contact_params
params.require(:contact).permit(:name, :email, :kind_id, :rmk, address_attributes: [:street, :city, :state])
end
end
@brandondees
Copy link

you're calling @kind_options_for_select directly from the form, but not the method that initializes it, as far as i can tell

@crova
Copy link
Author

crova commented Apr 5, 2018

I thought I was doing that on lines 21 and 26 of on the controller

@brandondees
Copy link

you're doing it for new and edit actions which render the form initially, but you aren't doing it on create and update actions which both re-render the form in case of any input error

@crova
Copy link
Author

crova commented Apr 5, 2018

Ohh, I see, rookie mistake

Cheers mate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment