secret
Created

Passing nil into "connection" Object

  • Download Gist
connection.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
class Connection < ActiveRecord::Base
attr_accessible :reason, :established, :connector, :connectee1, :connectee2,
:connectee1_attributes, :connectee2_attributes, :connector_attributes
belongs_to :connector, class_name: "User"
belongs_to :connectee1, class_name: "User"
belongs_to :connectee2, class_name: "User"
 
accepts_nested_attributes_for :connector, :connectee1, :connectee2
validates :reason, :presence => true
validates_length_of :reason, :maximum => 160
 
after_create :mail_connectees
after_initialize :build_associated_parties
after_create :log_successful_mail
# Builds connectee's and connector objects
def build_associated_parties
build_connector
build_connectee1
build_connectee2
end
 
# Sends email to both connectee1 and connectee2 with offer to connect #
def mail_connectees
ConnectionMailer.connectee1_email(connectee1).deliver
ConnectionMailer.connectee2_email(connectee2).deliver
end
 
def log_successful_mail
Rails.logger.info "Mailed successfully!"
end
 
end
connections_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
class ConnectionsController < ApplicationController
 
# instantiate new connection object #
def new
@connection = Connection.new
@connection.build_connectee1
@connection.build_connectee2
@connection.build_connector
end
# save attributes to connection #
def create
debugger
@connection = Connection.new params[:connection]
 
if @connection.save
flash[:notice] = "Connection created successfully!"
redirect_to @connection
else
render :new
end
end
 
def edit
@connection = Connection.find(params[:id])
end
def update
end
def show
Connection.find(params[:id])
end
end
new.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<h1>Tell Us 2 People to Introduce</h1>
 
<div class="errors">
<% if @connection.errors.present? %>
<% @connection.errors.each do |key, message| %>
<p class="error"><b><%= key %></b>: <%= uppercase.message %></p>
<% end %>
<% end %>
</div>
 
<%= form_for @connection do |f| %>
<h2>Tell us two poeple you want to connect?</h2>
<%= f.fields_for :connectee1 do |c1| %>
<%= c1.label :name1 %>
<%= c1.text_field :name%>
<%= c1.label :email1 %>
<%= c1.email_field :email%>
<% end %>
<%= f.fields_for :connectee2 do |c2| %>
<%= c2.label :name2 %>
<%= c2.text_field :name%>
<%= c2.label :email2 %>
<%= c2.email_field :email%>
<% end %>
<h2>Who should get the credit?</h2>
<%= f.fields_for :connector do |c| %>
<%= c.label :name %>
<%= c.text_field :name%>
<%= c.label :email %>
<%= c.email_field :email%>
<% end %>
<%= f.label :reason %>
<%= f.text_area :reason %>
<%= f.submit "Introduce" %>
<% end %>

I'm building the new variables in new method, but values are not getting passed to create method.

For some reason when I build the nested attributes for @connection in the new method, the values are not being passed to my create method (i think).

the error was from my method

after_initialize :build_associated_parties

it was not executing the following properly

build_connector, build_connectee1, build_connctee2
I moved it to connections_controller and corrected the error. Don't understand why, but it does.

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

As soon as new method is called, those variables are instantiated and your new page is then rendered. All those variables will be present only in your new scope. When you submit the page/form on that page with some action, your create is called. create will only receive the parameters (params) passed on to it by the page; and hence will not retain variables from new. :) Class variables may help you out here. Looking at code again.

Can you paste in or screenshot the error backtrace you're getting?

I think I know what was happening here. I think the build_* calls were overwriting the values passed in from the form. I think that method should probably be this:

def build_associated_parties
  build_connectee1 unless connectee1
  build_connectee2 unless connectee2
  build_connector unless connector
end

@ajsharp - you were spot on.

Now I need to search through the API docs to find out why?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.