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
task / show.html.erb
<h1>Task Summary</h1>
<%= render 'shared/navbar' %>
Title: <%[email protected]%> <br/>
Details: <%[email protected]%> <br/>
Requested by: <%[email protected]_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
通过将user_id
of附加current_user
到数组来更新模型中的数组。但是,当我测试它并单击链接到update
操作的“接受任务”时,没有任何反应。我该如何解决?
update
由于未指定使用“ PUT”方法,因此“接受任务”链接将不会链接到操作。相反,它将链接到“显示”操作。
而且,您正在传递current_user.profile
给tasks_path
助手-这不会为您提供正确的RESTful地址。更改链接,使其看起来像这样:
<%= link_to "Accept Task", task_path(@task), :method => :put %>
甚至更简单:
<%= link_to "Accept Task", @task, :method => :put %>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句