[英]Error With Post Request and Relationships Ruby On Rails
I have a rails project where there is a Customer table and a Ticket table. 我有一个Rails项目,其中有一个Customer表和Ticket表。 The relationship is Ticket has_many :customers and Customer belongs_to :ticket.
该关系为票证has_many:customers和客户属地_to:ticket。
On my Customer index page I list out all of the customers with create, update, and delete options. 在我的客户索引页面上,我列出了所有具有创建,更新和删除选项的客户。 All of this works fine.
所有这些都很好。 However, what I am wanting to do now is create another button (on the index page for each customer) that when clicked, it creates a Ticket with the customer selected added to the ticket.
但是,我现在想要做的是创建另一个按钮(在每个客户的索引页面上),单击该按钮时,它将创建一个故障单,并将选定的客户添加到故障单中。 I currently have the relationships setup in the models.
我目前在模型中设置了关系。 My first idea was to make a add a button for each customer in the table and have that button make a post request to my create function in my Tickets controller like this
我的第一个想法是为表中的每个客户添加一个按钮,并让该按钮向我的Tickets控制器中的create函数发出发布请求,如下所示
post 'tickets', to: 'tickets#create', as: 'create_ticket'
When the ticket function is invoked, I assumed that I would get the customer from the post request, create a new ticket, and somehow make the ticket take ownership of the customer with my relationship. 调用票证功能时,我假设我将从发布请求中获取客户,创建新票证,并以某种方式使该票证通过我的关系获得客户的所有权。 Here is my create function.
这是我的创建功能。
def create
@customer = Customer.find(params.require(:customer))
end
So far I have only been able to get errors like "ActionController::ParameterMissing in TicketsController#create param is missing or the value is empty: customer" 到目前为止,我只能收到类似“ TicketsController#create参数中的ActionController :: ParameterMissing丢失或值为空:customer”的错误。
Here is the link in my index.html.erb that is making the post request 这是发出发布请求的index.html.erb中的链接
<%= link_to "add to tickets", create_ticket_path(c), method: :post %>
Any help would be appreciated! 任何帮助,将不胜感激! Thanks in advance.
提前致谢。
I have just recently tried implementing this in a similar way to my edit function. 我最近刚刚尝试以类似于编辑功能的方式实现此功能。
My link in the index.html.erb is now 我在index.html.erb中的链接现在
<%= link_to "add to tickets", create_ticket_path(c) %>
My route is now 我的路线现在
get 'tickets/:id/create', to: 'tickets#create', as: 'create_ticket'
When I click the button this is the url that my browser directs to 当我单击按钮时,这是我的浏览器指向的URL
http://localhost:3000/tickets/1/create
and my create function in the Tickets controller is now 现在我在Tickets控制器中的创建功能
def create
@customer = Customer.find(params[:id])
end
This is the error I am now getting: 这是我现在得到的错误:
Template is missing Missing template tickets/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. 模板丢失缺少模板票证/创建,应用程序/使用{:locale => [:en] 、: formats => [:html] 、: variants => [] 、: handlers => [:erb,:builder, :raw,:ruby,:coffee,:jbuilder]}。 Searched in: * "/vagrant/ServiceManager/app/views" * "/home/vagrant/.rvm/gems/ruby-2.2.1/gems/devise-3.5.2/app/views" Extracted source (around line #46):
在以下位置搜索:*“ / 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 %>
Here are my model associations: 这是我的模型关联:
class Ticket < ActiveRecord::Base
has_many :customers
end
class Customer < ActiveRecord::Base
belongs_to :ticket
end
Here is the content of the index.html.erb 这是index.html.erb的内容
Description: Name: Customer 说明:名称:客户
The name should be bob. 名字应该是鲍勃。 It does the same thing for all customers.
它对所有客户都做同样的事情。
You can do something like: 您可以执行以下操作:
@ticket = Ticket.new(customers: [@customer])
if @ticket.save
redirect_to @ticket
else
render :new # This can be a render or redirect or whatever should happen if it fails
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.