0

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
  • The params that the form is submitting has a params for contact and params for call. So the data is getting to the server. What does your contact#edit controller method look like? – evanbikes Dec 30 '14 at 22:15
  • My guess would be that the form_for path is pointing to the wrong action. You could specify the action in form_for using :action => 'your action' to make sure its pointing to the correct action. [Documentation](http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html) – trueinViso Dec 31 '14 at 01:20
  • @evanbikes I added the edit contact controller method. – Casanova Tapia Dec 31 '14 at 01:46
  • @trueinViso I added this to the form partial `= form_for [Contact.find(params[:id]), Contact.find(params[:id]).calls.new], :url => {action: "create"} do |f|` but still getting the same result. Params are getting submitted, but Call object is not getting created. – Casanova Tapia Dec 31 '14 at 01:48
  • I would specify the controller in the action, is it Call? Try :url => url_for(:controller => 'mycontroller', :action => 'myaction') This is from [here](http://stackoverflow.com/questions/5320414/form-for-but-to-post-to-a-different-action) – trueinViso Dec 31 '14 at 01:51
  • @trueinViso Added this `form_for [Contact.find(params[:id]), Contact.find(params[:id]).calls.new], :url => url_for(controller: "Call", action: "create") do |f|` and now getting UrlGenerationError in Contacts#edit, No route matches {:action=>"create", :controller=>"Call", :id=>"57"}. I updated the question to include my routes.rb, and also verified that I have a contacts#create path. – Casanova Tapia Dec 31 '14 at 02:03
  • Can you show the updated form too where you entered in the action method? – trueinViso Dec 31 '14 at 04:14
  • @trueinViso I edited the question to show the updated form partial. Thanks for sticking with this question! – Casanova Tapia Jan 01 '15 at 15:38
  • Not sure if you are still working on this, but I had an idea. I was reading the source rails documentation and thought maybe you were passing the nested attribute incorrectly, like this instead: `form_for([Contact.find(params[:id]), Call.new]) do |f|`. The documentation is [here](https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/form_helper.rb) around line 244 it has examples of how to use form_for. I'm definitely not an expert on this, I usually end up getting stuff to work with trial and error. – trueinViso Jan 05 '15 at 02:49

0 Answers0