Ajax导致错误:在表单提交上启动POST“/ [object%20Object]”(Ajax causing error: Started POST “/[object%20Object]” on form submit)
我正在使用Rails创建一个能够提交联系信息和创建联系对象的页面。 联系人创建的控制器是:
def create @contact = Contact.new(contact_params) if @contact.save render json: @contact, status: :created else render json: @contact.errors.full_messages, status: :unprocessable_entity end end
与contact_params:
def contact_params params.require(:contact).permit(:name, :email, :phone, :company, :message) end
如果我处理重定向的所有内容,表单提交并创建对象,但我想用JS处理创建。 我之前在表单提交时使用过这种方法没有问题,但出于某种原因我运行时:
$(document).ready(function(){ $('#new_contact').on('submit', function(ev){ ev.preventDefault() $.post({ url: $(ev.target).attr('action'), data: new FormData(ev.target), processData: false, contentType: false, success: function(data){ $('#new_contact').append('<div class="col-xs-12 col-sm-6 col-sm- offset-3"><h3>Thanks for contacting me,' + data.name + '. I will get back to you as soon as I can.</h3></div>') document.getElementById('new_contact').reset() $('#new_contact input[type="submit"]').prop('disabled', false) }, error: function(error){ $('#new_contact').append('<div class="col-xs-12 col-sm-6 col-sm-offset-3"><h3>Sorry, I need at least a name and valid e-mail.</h3></div>') $('#new_contact input[type="submit"]').prop('disabled', false) } }) }) })
它命中了post请求,我的服务器返回错误:
ActionController :: RoutingError(没有路由匹配[POST]“/ [object%20Object]”):
我之前从未遇到过此错误,我不确定如何修复它。 任何帮助表示赞赏。
I'm using Rails to create a page with the ability to submit contact information and create a contact object. The controller for the Contact creation is:
def create @contact = Contact.new(contact_params) if @contact.save render json: @contact, status: :created else render json: @contact.errors.full_messages, status: :unprocessable_entity end end
with contact_params of:
def contact_params params.require(:contact).permit(:name, :email, :phone, :company, :message) end
The form submits and creates objects fine if I handle everything a redirect but I want to handle the creation with JS. I've used this method before on form submission with no problems but for some reason when I run:
$(document).ready(function(){ $('#new_contact').on('submit', function(ev){ ev.preventDefault() $.post({ url: $(ev.target).attr('action'), data: new FormData(ev.target), processData: false, contentType: false, success: function(data){ $('#new_contact').append('<div class="col-xs-12 col-sm-6 col-sm- offset-3"><h3>Thanks for contacting me,' + data.name + '. I will get back to you as soon as I can.</h3></div>') document.getElementById('new_contact').reset() $('#new_contact input[type="submit"]').prop('disabled', false) }, error: function(error){ $('#new_contact').append('<div class="col-xs-12 col-sm-6 col-sm-offset-3"><h3>Sorry, I need at least a name and valid e-mail.</h3></div>') $('#new_contact input[type="submit"]').prop('disabled', false) } }) }) })
It hits the post request and my server returns with the error:
ActionController::RoutingError (No route matches [POST] "/[object%20Object]"):
I've never encountered this error before and I'm not sure how to go about fixing it. Any help is appreciated.
原文:https://stackoverflow.com/questions/39334455
最满意答案
好的风格不是首先使用维度语句。 特别是如果你使用隐式类型。 每个变量都应该有一个声明的类型,最好将数组维度放在那里。 使用带有类型声明的属性(Fortran 90+)。
real :: energy_t(20000), nrt(20000) real, dimension(1000) :: npsh, xx, yy, step, stepz, r1, rr, ic, diffrr
保持线条不要太长。 声明尺寸(形状)的两种方式都是可能的。
如果你需要Fortran 77,你会受到更多限制,但仍然如此
real energy_t(20000), nrt(20000) real npsh(1000), xx(1000), yy(1000), step(1000), stepz(1000) real r1(1000), rr(1000), ic(1000), diffrr(1000)
可能更好。
尝试将相关变量分组到一行,将其他变量分组到不同的行。
我还建议声明大小为1000和20000的
parameter
常量。Good style is not to use the dimension statement in the first place. Especially if you use implicit typing. Every variable should have a declared type and is better to put the array dimension there. Use attributes with the type declaration (Fortran 90+).
real :: energy_t(20000), nrt(20000) real, dimension(1000) :: npsh, xx, yy, step, stepz, r1, rr, ic, diffrr
Keep lines not too long. Both ways of declaring size (shape) are possible.
If you need Fortran 77, you are more limited, but still
real energy_t(20000), nrt(20000) real npsh(1000), xx(1000), yy(1000), step(1000), stepz(1000) real r1(1000), rr(1000), ic(1000), diffrr(1000)
is probably better.
Try to group related variables on one line and the others on different lines.
I would also suggest to declare
parameter
constants for the sizes 1000 and 20000.
相关问答
更多-
我的5美分 Fortran Wiki是一个很好的起点。 它有关于使用现代Fortran编程的不同方面的文章。 单元测试,调试,通用编程等。同样非常有趣的表格可以在Fortran 2003 Standard编译器支持。 (正如Blklight已经提到的一些2003功能在编译器中不可用,它是比较不同编译器的好地方。) 我是一个C ++的人,但我被困在一些F90项目中。 我建议阅读本课程: 现代Fortran简介 。 MSB提到着名的“Fortran 95/2003解释”,但这本书足够大,内容充实。 相反,上述课 ...
-
毫无疑问,它是一个变量,它具有用于测量浮点计算的“精度” - 由于某些浮点数的不精确表示,它可能会漂移。 在Fortran中,除非使用编译器选项(或IMPLICIT语句)打开此功能,否则不必声明变量。 以AH和OZ开头的变量会自动考虑REAL变量(IN是整数)。 有问题的编译器可能会或可能不会将变量的内存清零,所以除非它被分配到某处 - 通常是COMMON块 - 它可能是零或某个任意值。 It's undoubtedly a variable holding an "accuracy" used to me ...
-
Fortran中的变量类型(Types of variables in Fortran)[2021-10-22]
这是简短的: real(kind=kr8) :: rnum 在这种情况下,变量或参数kr8在代码中的其他位置指定 - 如果在同一过程中,则在此行的上方;如果通过模块访问,则在不同的文件中。 从它的名字中,可以假设kr8是以这样的方式定义的,即对于某些特定的编译器,它具有双精度浮点数(8字节大小)。 请参阅Fortran编译器文档,了解kind参数对于各种精度和整型和实型变量的范围可以采用的值。 如果你使用强制,你的编译器是gfortran,g95或g77。 kind参数是引入到Fortran 90的一项功 ... -
这条线 COMMON/XQANG/SUM(300,10),MDIR,COSTH 通知子程序有一个叫做XQANG的公共块 ,它有一个名为COSTH的元素。 在没有其他信息的情况下,以及该年份的代码中,这很可能是一个real变量。 通用块是早期的Fortran机制,用于跨程序单元共享变量,例如在主程序和子程序之间共享变量。 直接使用相同的公共块声明将在多个位置找到,并具有相同的变量列表。 每个声明都指向相同的变量。 尽管如此,还有一个值得注意的地方。 公共块实际上是共享的内存块,并且不要求每个声明实例标识相同 ...
-
这主要是由于历史原因。 旧编译器(Fortran IV(66)及之前)实现了主要使用静态内存的程序。 旧机器甚至没有任何堆叠。 因此,程序的行为,因为变量被定义为save 。 变量初始化的前身DATA语句更像是定义静态内存的初始内容(类似于汇编中数据段的指令),这与您从C中知道的调用变量初始化相似。语法变得相似到后来的C变种。 This is mainly due to historical reasons. Old compilers (Fortran IV(66) and before) were im ...
-
同时引入的result是递归 。 在我们讨论递归函数如何出现之前,对结果变量的含义进行一些澄清。 无论是否使用结果,函数结果始终通过结果变量返回。 1使用result ,结果变量具有指定的名称,如果没有结果变量,则结果变量具有与函数相同的名称。 在后一种情况下,名称的使用是对结果变量的引用,而不是对函数的引用。 所以,如果函数foo有结果变量foo那么我们就不能做直接递归: recursive function foo(n) foo = foo(n-1) ! Oh dear end function ...
-
错误消息非常明确。 在变量声明中使用的初始化器必须是常量值。 在你的例子中, a不是一个常量。 它应该像这样工作: PROGRAM test3 IMPLICIT NONE INTEGER, PARAMETER :: a=2286 INTEGER :: b=a/3 WRITE(*,*) a, b END PROGRAM test3 因为那时a是一个常数。 The error message is pretty explicit. Initializers using in variabl ...
-
好的风格不是首先使用维度语句。 特别是如果你使用隐式类型。 每个变量都应该有一个声明的类型,最好将数组维度放在那里。 使用带有类型声明的属性(Fortran 90+)。 real :: energy_t(20000), nrt(20000) real, dimension(1000) :: npsh, xx, yy, step, stepz, r1, rr, ic, diffrr 保持线条不要太长。 声明尺寸(形状)的两种方式都是可能的。 如果你需要Fortran 77,你会受到更多限制,但仍然如此 re ...
-
使用cmake与fortran(Using cmake with fortran)[2024-05-06]
语言名称区分大小写。 发布后,我得到: ~/cmt> cmake ./ CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly. Missing variable is: CMAKE_fortran_COMPILER_ENV_VAR CMake Error: Error required internal CMake variable not set, cmake may ... -
这是相同的代码,只是现代化。 正如你所看到的那样,它需要一个a , b和c的实数数组,但是FORTRAN非常棒,因为你可以像对待数组那样处理标量 pure subroutine ilocz (a,b,c,n,m) implicit none ! Arguments integer, intent(in) :: n,m real, intent(in) :: a(n,n),b(n,m) real, intent(out) :: c(n,m) ! Local Vars integer :: i,j,k ...