我通过常规生成器scaffold_controller生成records_controller
。我想添加自定义方法,让我们调用它share
,以便只传递一个带有电子邮件地址的参数。如果我的数据库中存在电子邮件,我想在id
到record
内添加关联。
我向records_controller
添加了共享方法:
def share
logger.info record_params
logger.info params
# check email in db and add id
respond_to do |format|
format.js
end
end
显示视图上的表单:
<%= form_tag(controller: "records", action: "share", method: "put", remote: true) do %>
<%= label_tag(:user_email, "Share for:") %>
<%= text_field_tag(:user_email) %>
<%= submit_tag("Share") %>
<% end %>
并在路线中进行规则:
put '/records/:id/share', to: 'records#share'
表单呈现正常但是当我按下单击时出现错误:
ActionController::RoutingError (No route matches [POST] "/records/2/share"):
答案 0 :(得分:3)
根据form_tag
source code,您传递给帮助程序的参数都被视为url_for_options
并返回错误的输出:
<form action="/records/2/share?method=put&remote=true" method="post">
您需要指定哪些参数为url_for_options
,哪些参数仅为options
:
<%= form_tag({controller: "records", action: "share"}, method: 'put', remote: true) do %>