2017-12-09 9 views
0

私はまだRuby on Railsの基礎を学んでおり、軽量CRMを作成する際にエラーが発生しました。私は私の取引セクションのための簡単な作成操作を実行しようとすると、私は次のようなエラーに遭遇:ActionController :: UrlGenerationError-必須キーがありません:[:id]、一致しない可能性のある制約

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] 

以下は私のエラーのスクリーンショットです。

Error Screenshot

私はStackOverflowの周りを見て、Googleが間違って行くかもしれないかを理解するために検索し、私は解決策を見つけるように見えることはできません多くを行っています。私は現在、ここルビー2.4.1のレール5.1.4

を使用しています

は私のコントローラである:ここでは

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 

は私のルートです:

     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 

私はこれがあまりにも尋ねていないことを願っていますが、もしあなたがhav私の問題に対する答えが、私が間違っていたことを説明することを心に留めていますか?私はほとんどRoRについてできるだけ多くのことを学びたいと思っています。

ありがとうございました!

P.S:その他の情報が必要な場合は、お知らせください。

編集

は私のフォームを含んでいる私のnew.html.erbを追加しました:私はあなたの保存方法が真の私たちに与えていないと思う

<%= 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 %> 

答えて

0

を、何かが間違っています。新しいテンプレートで

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

のようなもの

<% 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 %> 
+0

オーケーということを変更してみましょう、私はちょうどあなたが提供するものに私のコントローラを更新し、それだけで新しいたびに戻ってレンダリングするようです。 – miklki14567

+1

新しいテンプレートのオブジェクトにエラーを表示するために何かを追加します。私は自分の答えを更新しました – Ursus

+0

よかった、素晴らしい!それは "接触が存在しなければならない"と言ったエラーを撃った。これは私のcontact_controllerについて話しているに違いない。私は "contact_id"がどこかに保存されていないと仮定しています... – miklki14567

関連する問題