【问题标题】:Rails form on ajax submission throws a 406 not acceptable errorajax 提交上的 Rails 表单引发 406 不可接受的错误
【发布时间】:2015-02-13 14:30:19
【问题描述】:

我是 Rails 新手,所以我可能以某种方式(显然)搞砸了。我有一个通过 ajax 将提交发送回控制器的表单。

这是那个文件 /index.html.erb/

<% provide(:title,'PlanYourTask') %>
<div class="mycontainer">
<div class="myprojcont container">
    <div class="row yourprojects" id="yourproject_<%=current_user.id%>">
        <% if @projects.any? %>
                <%@projects.each do |project| %>
                    <%=render 'projects/projects',:project => project %>
                <%end%>
        <%end%>
            <div class="col-md-3 col-xs-12 newproject">
                    <a class="btn btn-warning" style="border-bottom-left-radius:0px;border-bottom-right-radius:0px;display:block;">Create New Project
                    </a>

                <div class="dropdownnewproject">
                    <div class="projectdropdown" style="background:#bbb;border-bottom-left-radius:5px;border-bottom-right-radius:5px;margin:auto;padding:2px;
                    display:block;">
                    <p class="example-description">Enter the name of the project</p>
                    <%=form_for Project.new,:remote => true do |f|%>
                        <%=f.text_field :name,class:'form-control'%>
                        <%=f.submit class:'col-md-6 btn btn-success'%>
                    <%end%>
                    <a href="#" class="closeproject"><i class="fa fa-times fa-lg" style="margin:10px auto;"></i></a>
                    </div>
                </div>
            </div>
                <script type="text/javascript">
                    $('.newproject > a').click(function(e){
                        $('.dropdownnewproject').slideDown(1000);

                        e.preventDefault();
                    });
                    $('.closeproject').click(function(e){
                        $('.dropdownnewproject').slideUp(1000);
                        e.preventDefault();
                    });
                </script>



    </div>
</div>
</div>

如果您可以看到其中有一个表单,它会发送一个 ajax 请求以创建新项目。

项目控制器在这里

class ProjectsController < ApplicationController
    before_filter :require_user
    def new
        @projects=current_user.projects
        @project=current_user.projects.new
    end

    def create
        @project=current_user.projects.create(params[:project])


        respond_to do |format|
        if @project.save
            format.html{redirect_to :projects}
            format.js

            flash[:success]="Project created successfully"
        else
            render new
        end
    end

    end

    def show
        @project=Project.find(params[:id])

    end

    def index
        @projects=current_user.projects
    end




end

最后是我的 create.js.erb 文件

$('#yourprojects_<%=current_user.id%>').append('<%=j render partial:"projects/projects", object: @project %>');

/projects/projects.html.erb/

<div class="col-md-3 projects" style="margin-bottom:15px;">
<p style="font-family:TimesNewRoman;font-size:25px;margin:25px auto;text-align:center;"><%=link_to project.name,project,style:'text-decoration:none;'%></p>
</div>

/Project.rb 文件/

class Project < ActiveRecord::Base
    extend FriendlyId

    friendly_id :name, use: :slugged
    validates :name,presence: :true
    validates :description,presence: :true
    has_many :tasks,dependent: :destroy
    has_many :assigns,dependent: :destroy
    has_many :subtasks,through: :tasks
    has_many :users,through: :assigns


    after_create :add_tasks

    private

    def add_tasks
        self.tasks.build(taskname: "Production")
        self.tasks.build(taskname: "Development")
        self.tasks.build(taskname: "Test")
    end 

end

当我在 chrome 中检查我的网络选项卡时单击提交按钮,它会向 /projects 发送一个发布请求,但错误是 406 不可接受。

谁能帮帮我。

【问题讨论】:

  • 您使用的是哪个版本的导轨?
  • 我使用的是 rails 3.2.1
  • 你能从你的模型 projects.rb 中发布代码吗?
  • 我已经添加了那个文件,请看一下
  • 查看强参数:github.com/rails/strong_parameters 你的控制器中缺少几行。

标签: ruby-on-rails ajax http-status-code-406


【解决方案1】:

尝试将此添加到您的模型中:

attr_accessible :name, :description, :tasks, :assigns, :subtasks, :users

您需要在此 attr_accessible 语句中包含您计划更新的所有属性。

这是 Rails 中的一项安全功能(在 Rails 4 中更改为强参数)。

【讨论】:

    【解决方案2】:

    ProjectsController#create 上,您应该在@project.save 条件下为else 块提供format.js 块。例如:

    if @project.save
      format.html { redirect_to :projects }
      format.js
    else
      // add these two formats
      format.html { render :new }
      format.js { json: @project.errors, status: :unprocessable_entity }
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-18
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      • 2014-09-27
      相关资源
      最近更新 更多