我仍然在轨道上学习ruby的基础知识,并在创建轻量级CRM时遇到错误。当我尝试为我的交易部分执行简单的创建操作时,我遇到以下错误:
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]
以下是我的错误截图。
我查看了StackOverflow并完成了许多Google搜索,以了解可能出现的问题,而且我似乎无法找到解决方案。
我目前正在使用 Ruby 2.4.1 和 Rails 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
我希望这不是要求太多,但如果你能解释我的问题的答案,解释我做错了什么?我主要是想尽可能多地学习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 :(得分:1)
我认为你的保存方法没有给我们真实,有些事情是错的。让我们修改像
这样的东西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 %>