表单在由ajax部分Ruby创建时有错误的操作
Form has the wrong action when created by an ajax partial - Ruby
我有一个由ajax部分加载的表单。当它被交换时,加载在ajax部分上的表单针对的是错误的控制器/url。我试图以各种方式瞄准正确的控制器,但它总是被重写到同一个不正确的控制器/url。
此外,如果表单是在页面刷新时加载的,那么它就会正确加载——只有当它是通过ajax加载的时,它才会针对不正确的控制器。
以下是表单html加载错误时的样子:
<form class="new_prediction" id="new_prediction" action="/predictions" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="✓">
以下是表单html的外观,以及页面刷新后的外观:
<form class="new_predictionend" id="new_predictionend" action="/predictionends" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="✓">
我的问题是,当表单由ajax加载时,为什么表单的目标是不正确的控制器"/prodictions"?分部清楚地表明,一个新的"Predictionand"模型是表单的对象,因此应该像刷新页面时正确地那样以"/prodictionends"为目标。
以下是为页面刷新和ajax加载加载的表单部分:
<%= form_for(Predictionend.new, remote:true) do |f| %>
<div class="sidebar-open-close-comment-div">
<%= f.text_area :comment, placeholder: "Make a closing comment", class: "close-comment-textarea" %>
</div>
<div class="sidebar-close-button">
<%= f.submit "SUBMIT", class: "btn" %>
</div>
<% end %>
这是加载表单的ajax文件:
$('.stockpage-sidebar-prediction').first().html("<%= j render partial: 'predictions/prediction_end', locals: {prediction: @prediction} %>");
加载ajax的控制器:
class PredictionsController < ApplicationController
def create
@prediction = @user.predictions.build(prediction)
@prediction.save
respond_to do |f|
f.js {
if invalid_start
render 'shared/_error_messages.js.erb'
else
render "predictions/create.js.erb"
end
}
end
end
end
路线:
post 'predictions' => 'predictions#create'
get 'predictions/:id' => 'predictions#show'
get 'predictions/hover/:id' => 'predictions#hover'
post 'predictionends' => 'predictionends#create'
如有任何帮助,我们将不胜感激。
我们这里有两个选项,请在下面找到。
=form_for@model,:url=>predictions_url,:remote=>true do|f|
或
=form_tag(predictions_url,:method=>"post",:id=>"model_form",:remote=>true)do|f|
相关文章:
- 回复'js'仅当请求有错误时(否则使用html)
- 表单已发送,但验证有错误
- 当我的表单中已经有一个操作时添加JavaScript
- 如果文件不存在,fs.watch是否有错误处理程序
- React+Redux-未捕获错误:操作可能没有未定义的“;类型“;所有物
- Meteor上有错误的同步问题
- 表单验证:如果有错误,不要't使手风琴动起来
- 我有错误文档.注册元素未被细化
- 如何转换为特定于 json 的类 c#?我有错误
- 如果当前输入有错误或警告,则禁用所有下一个表单输入字段
- 通过有错误的 Ajax-Request 打印出 HTML 会阻止进一步的 JS 工作
- 脸书应用程序有错误
- XDomainRequest 总是有错误
- HTML5 Canvas游戏有错误,点击时第一个拼图块会变成不同的块
- Firefox 安全错误:“操作不安全
- 错误:操作不安全.图片未在亚马逊服务器网址的画布 base64 中转换
- Page_ClientValidate返回 false,但没有一个验证器有错误
- 量角器由中继器命令中是否有错误
- 错误操作控制器::在轨道中呈现部分单击时返回link_to未知格式
- Jquery 验证器 - 表单被保存但有错误