2014-09-23 90 views
0

我有更新操作的問題 - 對於來自REST的PUT方法。 當我嘗試更新一行(在我的情況下,任務)它保持不變。沒有什麼變化。更新操作不更新數據

我的應用程序是一個簡單的'TO DO'列表。用戶可以創建任務,編輯和瀏覽任務。

我有控制器:

class TasksController < ApplicationController 

    def index 
    @task = Task.new 
    @tasks = Task.all 
    end 


    def create 
    Task.create(task_params) 
    redirect_to :back 
    end 

    def edit 
    @task = Task.find(params[:id]) 
    end 

    def update 
    @task = Task.find(params[:id]) 
    @task.save 
    redirect_to tasks_path 
    end 

    private 
    def task_params 
    params.require(:task).permit(:task) 
    end 

end 

和型號:

class Task < ActiveRecord::Base 
end 

和部分:

<%= form_for @task do |f| %> 
    <%= f.label :task, "Type your task:", id: "task" %> 
    <%= f.text_field :task %> 
    <%= f.submit "Create!", id: "add_task" %> 
<% end %> 

對於這樣的觀點:

<h1>Edit</h1> 
<%= render 'form' %> 

回答

4

您需要更新@task與PARAMS

def update 
    @task = Task.find(params[:id]) 
    if @task.update_attributes(task_params) 
    redirect_to tasks_path 
    else 
    render action: :edit 
    end 
end 
0

您必須設置任務的新值。使用find您只需獲取記錄,而不對現有對象執行任何實際更新。