我有一個rails項目,其中有一個Customer表和一個Ticket表。關係是Ticket has_many:customers和Customer belongs_to:ticket。與發佈請求和關係的錯誤Ruby On Rails
在我的客戶索引頁面上,我列出了所有具有創建,更新和刪除選項的客戶。所有這些工作正常。但是,我現在想要做的是創建另一個按鈕(在每個客戶的索引頁面上),單擊它時,會創建一個Ticket,並將選定的客戶添加到票證中。我目前在模型中建立了關係。我的第一個想法是做一個爲表中的每個客戶添加一個按鈕,並有一個按鈕,使POST請求我的創造功能在我的門票控制器這樣
post 'tickets', to: 'tickets#create', as: 'create_ticket'
當調用票功能,我認爲我會從發佈的請求中獲取客戶,創建新的票據,並以某種方式使票據以我的關係獲得客戶的所有權。 這是我的創建功能。
def create
@customer = Customer.find(params.require(:customer))
end
到目前爲止,我只能夠得到這樣的錯誤「在TicketsController的ActionController :: ParameterMissing#創建 參數是丟失或爲空值:客戶」
這裏是我的索引鏈接.html.erb即拍即POST請求
<%= link_to "add to tickets", create_ticket_path(c), method: :post %>
任何幫助,將不勝感激!提前致謝。
我剛纔最近試圖以類似的方式,以我的編輯功能實現這一點。
我在index.html.erb鏈接現在是
<%= link_to "add to tickets", create_ticket_path(c) %>
我現在的路線是
get 'tickets/:id/create', to: 'tickets#create', as: 'create_ticket'
當我點擊按鈕,這是我的瀏覽器引導到
網址http://localhost:3000/tickets/1/create
和我在門票控制器中的創建功能現在是
def create
@customer = Customer.find(params[:id])
end
這是我現在得到的錯誤:
模板丟失 缺少模板門票/創建,應用程序/用{創建:區域設置=> [:EN]:格式=> [:HTML ],:variants => [],:handlers => [:erb,:builder,:raw,:ruby,:coffee,:jbuilder]}。搜索:*「/ vagrant/ServiceManager/app/views」*「/home/vagrant/.rvm/gems/ruby-2.2.1/gems/devise-3.5.2/app/views」 提取源代碼#46):
def find(*args)
find_all(*args).first || raise(MissingTemplate.new(self, *args))
end
def find_all(path, prefixes = [], *args)
<% @tickets.each do |t| %>
<tr>
<td><%= t.description %></td>
<td><%= t.customers.name %></td>
</tr>
<% end %>
這裏是我的模型關聯:
class Ticket < ActiveRecord::Base
has_many :customers
end
class Customer < ActiveRecord::Base
belongs_to :ticket
end
這裏是index.html的內容。ERB
:品名: 客戶
的名稱應該是鮑勃。它爲所有客戶都做同樣的事情。
你能後的'link_to'正在生成網址是什麼?你沒有通過一個'客戶'鍵傳遞一個json對象,這就是爲什麼你會得到這個錯誤。通常情況下,您將擁有一個帶有隱藏字段的customer ['id]屬性的表單,然後讓submit按鈕成爲實際點擊的按鈕或某種類型的按鈕。 – CWitty
@CWitty我剛剛編輯了這個問題。我所有的編輯都在水平線以下。 –
所以對於那個,你並沒有告訴它重定向或渲染任何東西,所以它正在尋找一個它找不到的模板。嘗試一個'redirect_to @ customer'或soemthing,並且應該修復它 – CWitty