2016-04-22 23 views
0

我無法獲取最佳位置以進行更新。這是我第一次使用這個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%肯定這是我在控制器中配置錯誤的東西,我只是不能爲我的生活弄清楚它是什麼。

編輯:這個背景是我有一堆'任務',我想列出他們所有的索引,並能夠通過點擊標題更新索引上的任何任務。

回答

0
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,它應該正確地發送請求。

+0

謝謝。我覺得很愚蠢。當然,這與你的建議非常吻合。 –

+0

太棒了。樂於幫助! –