Rails ParameterMissing的最佳位置

时间:2016-04-22 18:00:06

标签: ruby-on-rails ruby best-in-place

我无法获得Best In Place进行更新。这是我第一次使用这个宝石或制作一个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%肯定这是我在控制器中配置错误的东西,我不能为我的生活弄清楚它是什么。

编辑:这背景是我有一堆'任务',我想在索引上列出所有这些,并且只需点击标题即可更新索引上的任何任务。

1 个答案:

答案 0 :(得分:0)

string createTable = "Create Table [Sheet_X] (field1 char(255), field2 char(255),field3 char(255));";
OleDbCommand cmd = new OleDbCommand(createTable, conn);
conn.Open();
cmd.ExecuteNonQuery();
var counter = 0;
foreach (var item in items)
{
    if (conn.State == ConnectionState.Closed)
        conn.Open();
    string insertdata = "insert into [Sheet_X] (field1,field2,field3) values('value1','value2','value3');";
    counter++;
    if (counter >= 1000)
    {
        counter = 0;
        conn.Close();
    }
}

这告诉您没有名称为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" ,并且应该正确发送请求。