miklki14567
miklki14567

Reputation: 149

ActionController::UrlGenerationError- missing required keys: [:id], possible unmatched constraints

I am still learning the basics of ruby on rails and have ran into an error while creating a lightweight CRM. When I try to perform a simple create operation for my deals section I run into the following error:

ActionController::UrlGenerationError in DealsController#create

No route matches {:action=>"show", :contact_id=>"2", :controller=>"deals", :organization_id=>#<Deal id: nil, deal_name: "Amazon Deal", deal_amount: nil, contact_id: nil, created_at: nil, updated_at: nil>}, missing required keys: [:id], possible unmatched constraints: [:organization_id]

Below is my error screenshot.

Error Screenshot

I have looked around StackOverflow and done many Google searches to understand what might be going wrong and I can't seem to find a solution.

I am currently using Ruby 2.4.1 and Rails 5.1.4

Here is my controller:

class DealsController < ApplicationController

def index
@deals = Deal.all
end

def new
@deal = Deal.new
end

def show
@deal = Deal.find(params[:id])
end

def create
@deal = Deal.new(deal_params)
@deal.save

redirect_to organization_contact_deal_url(@deal)

end

private
def deal_params
params.require(:deal).permit(:deal_name, :deal_amount)
end
end

Here are my routes:

                        Prefix Verb   URI Pattern                                                                   Controller#Action
             welcome_index GET    /welcome/index(.:format)                                                      welcome#index
organization_contact_deals GET    /organizations/:organization_id/contacts/:contact_id/deals(.:format)          deals#index
                           POST   /organizations/:organization_id/contacts/:contact_id/deals(.:format)          deals#create
 new_organization_contact_deal GET /organizations/:organization_id/contacts/:contact_id/deals/new(.:format)      deals#new
 edit_organization_contact_deal GET    /organizations/:organization_id/contacts/:contact_id/deals/:id/edit(.:format) deals#edit
 organization_contact_deal GET    /organizations/:organization_id/contacts/:contact_id/deals/:id(.:format)      deals#show
                           PATCH  /organizations/:organization_id/contacts/:contact_id/deals/:id(.:format)      deals#update
                           PUT    /organizations/:organization_id/contacts/:contact_id/deals/:id(.:format)      deals#update
                           DELETE /organizations/:organization_id/contacts/:contact_id/deals/:id(.:format)      deals#destroy
     organization_contacts GET    /organizations/:organization_id/contacts(.:format)                            contacts#index
                           POST   /organizations/:organization_id/contacts(.:format)                            contacts#create
  new_organization_contact GET    /organizations/:organization_id/contacts/new(.:format)                        contacts#new
 edit_organization_contact GET    /organizations/:organization_id/contacts/:id/edit(.:format)                   contacts#edit
      organization_contact GET    /organizations/:organization_id/contacts/:id(.:format)                        contacts#show
                           PATCH  /organizations/:organization_id/contacts/:id(.:format)                        contacts#update
                           PUT    /organizations/:organization_id/contacts/:id(.:format)                        contacts#update
                           DELETE /organizations/:organization_id/contacts/:id(.:format)                        contacts#destroy
             organizations GET    /organizations(.:format)                                                      organizations#index
                           POST   /organizations(.:format)                                                      organizations#create
          new_organization GET    /organizations/new(.:format)                                                  organizations#new
         edit_organization GET    /organizations/:id/edit(.:format)                                             organizations#edit
              organization GET    /organizations/:id(.:format)                                                  organizations#show
                           PATCH  /organizations/:id(.:format)                                                  organizations#update
                           PUT    /organizations/:id(.:format)                                                  organizations#update
                           DELETE /organizations/:id(.:format)                                                  organizations#destroy
                      root GET    /                                                                             welcome#index

I hope this isn't asking too much but If you have the answer to my problem to mind explaining what I did wrong and such? I'm mostly trying to learn as much as I can about RoR.

Thank you!

P.S: If you need another other information please let me know.

Edit:

Added my new.html.erb which includes my form:

<%= form_with scope: :deal, url: organization_contact_deals_path, 
local: true do |form| %>

<p>
<%= form.label :deal_name %>
<%= form.text_field :deal_name %>
</p>

<p>
<%= form.submit %>
</p>

<% end %>

Upvotes: 2

Views: 1453

Answers (1)

Ursus
Ursus

Reputation: 30056

I think your save method is not giving us true, something is wrong. Let's modify that something like

def create
  @deal = Deal.new(deal_params)
  if @deal.save
    redirect_to organization_contact_deal_url(@deal)
  else
    render :new
  end
end

in new template

<% if @deal.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@deal.errors.count, "error") %> prohibited this user from being saved:</h2>

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

Upvotes: 1

Related Questions