Rails ParameterMissing 的最佳位置

Best In Place for Rails ParameterMissing

我无法更新 Best In Place。这是我第一次使用这个 gem 或制作一个 Rails 应用程序,所以我确定我只是遗漏了一些简单的东西,即使我已经在这里搜索了文档并搜索了其他问题.

这是我在控制台中得到的错误:

Started PUT "/tasks/1" for 127.0.0.1 at 2016-04-22 11:52:10 -0600
Processing by TasksController#update as JSON
  Parameters: {"authenticity_token"=>"cAwRbx8JeYDMG1LrR7nl0VQfwW/x00RUd8rsTP8Iwc
0=", "tasks"=>{"title"=>"Task 1sadsads"}, "id"=>"1"}
  Task Load (0.0ms)  SELECT  "tasks".* FROM "tasks"  WHERE "tasks"."id" = ?  ORD
ER BY priority ASC LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "tasks".* FROM "tasks"  WHERE "tasks"."id" = ?  ORDER B
Y priority ASC LIMIT 1  [["id", "1"]]
Completed 400 Bad Request in 3ms

ActionController::ParameterMissing (param is missing or the value is empty: task
):
  app/controllers/tasks_controller.rb:99:in `task_params'
  app/controllers/tasks_controller.rb:62:in `update'


  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
 (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (36.0ms)

我的控制器:

  class TasksController < ApplicationController
  before_action :set_task, only: [:show, :edit, :update, :destroy]

  # GET /tasks
  # GET /tasks.json
  def index
    @tasks = Task.all
    respond_to do |type|
      type.html
      type.json {render :json => @task}
    end
  end



  # GET /tasks/1
  # GET /tasks/1.json
  def show
          @task = Task.find params[:id]
    respond_to do |format|
      format.html
      format.json {render :json => @task}
    end
  end

  # GET /tasks/new
  def new
    @task = Task.new
  end

  # GET /tasks/1/edit
  def edit
  end

  # POST /tasks
  # POST /tasks.json
  def create
    @task = Task.new(task_params)

    respond_to do |format|
      if @task.save
        format.html { redirect_to @task, notice: 'Task was successfully created.' }
        format.json { render :show, status: :created, location: @task }
      else
        format.html { render :new }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

   def sort
    params[:order].each do |key,value|
      Task.find(value[:id]).update_attribute(:priority,value[:position])
    end
    render :nothing => true
  end

  # PATCH/PUT /tasks/1
  # PATCH/PUT /tasks/1.json
  def update
        @task = Task.find params[:id]
   if @task.update(task_params)
      respond_to do |format|
        format.html { redirect_to( @task )}
        format.json { render :json => @task }
      end
    else
      respond_to do |format|
        format.html { render :action  => :edit } # edit.html.erb
        format.json { render :nothing =>  true }
      end
    end
  end







  # DELETE /tasks/1
  # DELETE /tasks/1.json
  def destroy
    @task.destroy
    respond_to do |format|
      format.html { redirect_to tasks_url, notice: 'Task was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_task
      @task = Task.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def task_params
      params.require(:task).permit(:title, :description, :priority)
    end
end

我的部分观点:

    <div class="panel panel-default" data-id="<%= task.id %>">
      <div class="panel-heading">
        <h3 class="panel-title"><span class="rest-in-place" data-url="/tasks/<%= task.id %>" data-object="tasks" data-attribute="title" data-placeholder="Enter a title">
        <%= task.title %>
    </span></h3>
      </div>
      <div class="panel-body">
        <%= truncate task.description, length: 50 %>
      </div>
    </div>

我非常感谢能得到的任何帮助。我 99% 确定这是我在控制器中配置错误的东西,我就是一辈子都弄不明白它是什么。

编辑:这样做的背景是我有一堆 'Tasks',我想将它们全部列在索引中,并且只需单击标题就可以更新索引中的任何任务。

ActionController::ParameterMissing (param is missing or the value is empty: task
):
app/controllers/tasks_controller.rb:99:in `task_params'
app/controllers/tasks_controller.rb:62:in `update'

这是告诉您没有名称为 :task 的参数。您可以在这条消息上方看到您的参数。

Parameters: {"authenticity_token"=>"cAwRbx8JeYDMG1LrR7nl0VQfwW/x00RUd8rsTP8Iwc
0=", "tasks"=>{"title"=>"Task 1sadsads"}, "id"=>"1"}

您的参数中有 tasks。此信息来自您的观点。

data-object="tasks"

尝试将您视图中的这部分更改为 task,它应该会正确发送请求。