数据库中的数组未更新

时间:2013-11-20 12:31:37

标签: ruby-on-rails

task.rb

class Task < ActiveRecord::Base
    belongs_to :user
    attr_accessible :title, :details, :user_id
    serialize :volunteers,Array
end

TasksController

class TasksController < ApplicationController
    def new
        @task = Task.new
    end

    def create
        @task = Task.new(params[:task])
        @task.user_id = current_user.id
        if @task.save
            flash[:notice] = "Task successfully added"
            redirect_to static_pages_dashboard_path
         end
     end 

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

     def index
        @task = Task.all
     end

     def update
        @task = Task.find(params[:id])
        @task.volunteers << profile_path(current_user.profile)
        if @task.save
            flash[:notice] = "You have accepted the task"
        end
     end
end

任务/ show.html.erb

<h1>Task Summary</h1>
<%= render 'shared/navbar' %>
Title: <%=@task.title%> <br/>
Details: <%=@task.details%> <br/>
Requested by: <%=@task.user_id%> <br/>

<%= link_to "Accept Task", tasks_path(current_user.profile) %> 

的routes.rb

Test::Application.routes.draw do
   get "welcome/index"
   get "static_pages/home"
   get "static_pages/dashboard"
   get "tasks/index"
   root "static_pages#home"
   devise_for :users 
   resources :tasks
   resources :profiles
end


你好。我试图通过将volunteers的{​​{1}}附加到数组来更新模型中的数组user_id。但是,当我测试它并单击链接到current_user操作的“接受任务”时,没有任何反应。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

“接受任务”链接不会链接到update操作,因为您未指定使用“PUT”方法。它将改为链接到“show”动作。

此外,您正在将current_user.profile传递给tasks_path帮助程序 - 这不会为您提供正确的RESTful地址。将您的链接更改为:

<%= link_to "Accept Task", task_path(@task), :method => :put %> 

甚至更简单:

<%= link_to "Accept Task", @task, :method => :put %>