繁体   English   中英

加载资源失败:服务器响应状态为500(内部服务器错误)Ajax Rails错误

[英]Failed to load resource: the server responded with a status of 500 (Internal Server Error) Ajax Rails error

我目前正试图在我的Rails应用程序中添加一些Ajax。 尝试创建新帖子时出现此错误。 我知道帖子是创建的,因为当我刷新页面时,该帖子最终出现在帖子列表中。

posts_controller.rb

  def create
    @post = Post.new(post_params)
    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
        format.js
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
        format.js
      end
    end
  end

应用程序/视图/职位/ index.html.erb

<%= form_for(@post, remote: true) do |f| %>
      <div class="modal fade" id="mynewpost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <h4 class="modal-title" id="myModalLabel">Suggested Invite</h4>
            </div>
            <div class="modal-body">
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal" id="mynewpost">Close</button>
              <%= submit_tag "Create", class: "btn btn-primary" %>
            </div>
          </div>

应用程序/视图/职位/ create.js.erb

alert("Post Created!!");

的routes.rb

resources :posts

现在我希望单击“提交”后,应该会看到已创建poste!但是我没有。 我只是收到内部服务器错误。 从这里我不知道该怎么办。 请让我知道我是否可以提供其他信息。

看起来可能是语法错误

{ render :show, status: :created, location: @post }

从帖子中渲染json并控制javascript中的逻辑会更简单。

render json: @post

然后在javascript中有一个关于表单成功的侦听器

    $("form").on("ajax:complete", (xhr, status) ->
           alert("Post was created!");
    )
    $("form").("ajax:error", (xhr, data, status) ->
      "Handle errors here";
    )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM