gpt4 book ai didi

javascript - jQuery 验证提交表单 ActionController::ParameterMissing

转载 作者:太空宇宙 更新时间:2023-11-04 15:34:47 26 4
gpt4 key购买 nike

服务器日志:

`ActionController::ParameterMissing (param is missing or the value is empty: comment)`

使用 pry gem gem 参数:

 <ActionController::Parameters {"utf8"=>"✓", "comment_name"=>"123432",
"comment_description"=>"231424", "commit"=>"submit",
"controller"=>"comments", "action"=>"create", "article_id"=>"5"} permitted: false>

我知道:comment应该包装comment_namecomment_description

所以在验证添加submitHandler时尝试修复格式错误

点击浏览器显示的提交按钮:

enter image description here

jquery 验证:

$(function () {

$("form#new_comment").validate({
rules: {
comment_name: {
required: true,
minlength: 3
},

comment_description: {
required: true,
minlength: 5
},
submitHandler: function (form) {
$.ajax({
url: form.action,
type: form.method,
data: $(form).serializeArray(),
dataType: 'script'
});
}
}
});
});

_form.html.erb:

<%= simple_form_for [@article, @article.comments.build], remote: true do |f| %>
<%= f.input :name, input_html: { name: "comment_name"} %>
<%= f.input :description, input_html: { name: "comment_description" } %>
<%= f.submit :submit, class: "btn btn-default" %>
<% end %>

评论 Controller :

class CommentsController < ApplicationController

before_action :get_article
before_action :authenticate_user!

def create
@comment = @article.comments.create(comment_params)
@comment.user_id = current_user.id
if @comment.save
respond_to do |format|
format.html { redirect_to @article, notice: "creaed succeed"}
format.js { }
end
else
redirect_to @article, notice: "characters is too short or name has been taken"
end
end

def destroy
@comment = @article.comments.find(params[:id])
if @comment.destroy
respond_to do |format|
format.html { redirect_to @article }
format.js { }
end
end
end

private

def comment_params
params.require(:comment).permit(:name, :description, :article_id, :user_id)
end

def get_article
@article = Article.find(params[:article_id])
end
end

任何帮助谢谢🙃

最佳答案

Controller 期望

<ActionController::Parameters {"utf8"=>"✓", "comment"=>
{name"=>"123432","description"=>"231424"}, "commit"=>"submit",
"controller"=>"comments", "action"=>"create", "article_id"=>"5"}
permitted: false>

在您的表单中,通过为“名称”和“描述”字段声明“名称”属性,您实际上是将“名称”字段的名称从 comment[name] 覆盖为 评论名称。因此,只需从表单中删除这些名称属性

<%= simple_form_for [@article, @article.comments.build], remote: true do |f| %>
<%= f.input :name%>
<%= f.input :description%>
<%= f.submit :submit, class: "btn btn-default" %>
<% end %>`

关于javascript - jQuery 验证提交表单 ActionController::ParameterMissing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44480963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com