I have a Call model nested under Contact, a Contact has many calls. In the Edit#Contact view, I'm rendering the form to create a new call. When I submit the form, params are getting submitted, but the new Call object is not saved.
Note that if I just create a new Call from the New#Call view (form not rendered in Edit#Contact view), then the Call object is saved, so it's only a problem when rendering the form in the Edit Contact view.
console output:
Started PATCH "/contacts/54" for 127.0.0.1 at 2014-12-30 15:29:05 -0500
Processing by ContactsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wyo0S3G5NHdfFr3e2DFw/ArhA7ouKdljCP5XaHMQ5Gc=", "contact"=>{"name"=>"Sample User 3", "entity"=>"Company 3", "phone"=>"888-888-8888", "alt_phone"=>"", "dead_phone"=>"", "email"=>"sample3@example.com", "alt_email"=>"", "dead_email"=>"", "body"=>""}, "call"=>{"dial_id"=>"1", "conversation_id"=>"1", "investing_id"=>"1", "timing_id"=>"1", "motivator_id"=>"1"}, "commit"=>"Save", "id"=>"54"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Contact Load (0.1ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = ? ORDER BY updated_at DESC LIMIT 1 [["id", 54]]
(0.1ms) begin transaction
(0.1ms) commit transaction
Contact Store (725.1ms) {"id":54,"exception":["Elasticsearch::Transport::Transport::Errors::NotFound","[404] {\"error\":\"IndexMissingException[[contacts_development] missing]\",\"status\":404}"]}
[404] {"error":"IndexMissingException[[contacts_development] missing]","status":404}
Contact Load (0.5ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."user_id" = ? AND (id < 54) ORDER BY updated_at DESC LIMIT 1 [["user_id", 1]]
Property Load (0.2ms) SELECT "properties".* FROM "properties" WHERE "properties"."contact_id" = ? [["contact_id", 54]]
CACHE (0.0ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = ? ORDER BY updated_at DESC LIMIT 1 [["id", "54"]]
CACHE (0.0ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = ? ORDER BY updated_at DESC LIMIT 1 [["id", "54"]]
Dial Load (0.2ms) SELECT "dials".* FROM "dials"
Conversation Load (0.2ms) SELECT "conversations".* FROM "conversations"
Investing Load (0.2ms) SELECT "investings".* FROM "investings"
Timing Load (0.1ms) SELECT "timings".* FROM "timings"
Motivator Load (0.1ms) SELECT "motivators".* FROM "motivators"
Rendered calls/_form.html.haml (11.9ms)
Contact Load (0.5ms) SELECT "contacts".* FROM "contacts" ORDER BY updated_at DESC LIMIT 1
Contact Load (0.4ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."user_id" = ? ORDER BY updated_at ASC LIMIT 1 [["user_id", 1]]
Rendered contacts/edit.html.haml within layouts/application (29.7ms)
Contact Load (0.5ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."user_id" = ? ORDER BY updated_at DESC LIMIT 1 [["user_id", 1]]
CACHE (0.0ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."user_id" = ? ORDER BY updated_at DESC LIMIT 1 [["user_id", 1]]
Completed 200 OK in 1039ms (Views: 304.1ms | Searchkick: 725.1ms | ActiveRecord: 3.6ms)
Here is the updated form partial, rendered in contact#edit:
= form_for [Contact.find(params[:id]), Contact.find(params[:id]).calls.new], :url => url_for(controller: "Call", action: "create") do |f|
.form-group
= f.label :dial
= f.select :dial_id, options_from_collection_for_select(Dial.all, "id", "result"), class: 'form-control'
.form-group
= f.label :conversation
= f.select :conversation_id, options_from_collection_for_select(Conversation.all, "id", "result")
.form-group
= f.label :investing
= f.select :investing_id, options_from_collection_for_select(Investing.all, "id", "result")
.form-group
= f.label :timing
= f.select :timing_id, options_from_collection_for_select(Timing.all, "id", "result")
.form-group
= f.label :motivator
= f.select :motivator_id, options_from_collection_for_select(Motivator.all, "id", "result")
.actions
= f.submit 'Save', class: 'btn btn-success btn-sm'
Rendering the new call form in contact#edit view like this:
= render 'calls/form'
As a side question, I wasn't able to figure out the form_for path for this. So to tackle one problem at a time, I just have the very wordy path as a placeholder.
Here is the edit
method in the Contacts Controller:
def edit
@contact = Contact.find(params[:id])
@user = @contact.user
@new_call = @contact.calls.build
authorize @contact
end
create
method in the Call Controller:
def create
@contact = Contact.find(params[:contact_id])
@call = Call.new(call_params)
@call.contact_id = @contact.id
@call.user_id = current_user.id
if @call.save
flash[:notice] = "Your Call was successfully posted."
redirect_to edit_contact_path(@contact)
else
flash[:error] = "Your Call was not posted. Please try again."
redirect_to edit_contact_path(@contact)
end
end
routes.rb:
resources :contacts do
resources :properties
resources :calls
collection do
post :import
get :database
end
end