路线或applogic

时间:2011-08-18 14:01:36

标签: ruby-on-rails

试图让rails路由到“click”而不是得到它

有一个项目和一个任务模型:

class Task
  include Mongoid::Document

  field :title,                     :type => String
  has_many :projects
  belongs_to :user
end


class Project

  include Mongoid::Document

  field :title,                     :type => String
  has_and_belongs_to_many           :tasks
  belongs_to                        :user
end

我想将任务与项目“关联” 所以我在项目控制器中有这个:

def connect
  @project = Project.find(params[:id])
  @project.tasks_ids.push(params[:task_id])
  @project.save
  redirect_to project
end

用这条路线:

resources :projects do
    match 'connect/:id' => 'projects#connect', :as => :connect, :via => :put
    resources :tasks
end

我似乎无法在视图中使用此功能:

 = link_to 'Associate Task', project_connect_path(@task)

失败了:

没有路线匹配{:controller =>“projects”,:action =>“connect”}

2 个答案:

答案 0 :(得分:0)

尝试这样做:

resources :projects do
member do
 put 'connect'
end
resources :tasks
end

或者您可以在resources :projects do ...之上写下您的路线 这里是链接,你可以在那里阅读更多关于路线的信息:Rails routes

答案 1 :(得分:0)

resources :projects do
  member do
   put :connect
  end
  resources :tasks
end

您的路径应如下所示:

= link_to'Associate Task',project_connect_path(@ project,:task_id => @ task.id),:method => :放