rails-根据url将信息传递给控制器

时间:2015-02-23 15:43:17

标签: ruby-on-rails ruby ruby-on-rails-4

我有一个控制器用于" Productions"如果你转到localhost/productions,你会看到一个索引页面,你可以点击显示并查看该特定作品的展示页面。 每个作品都有一个唯一的ID,如036ea872f9011a7c,我希望我的用户能够将作品添加到制作中,如下所示:

localhost/productions/036ea872f9011a7c/fixtures/add
localhost/productions/036ea872f9011a7c/positions/add
localhost/productions/036ea872f9011a7c/dimmers/add
localhost/productions/036ea872f9011a7c/channels/add
localhost/productions/036ea872f9011a7c/etc/add

1 个答案:

答案 0 :(得分:1)

您应该使用如下所需的参数构建路径:

假设我们有分配给项目的任务

model project.rb:

class Project < ActiveRecord::Base
  has_many :tasks, through: :project_task
end

model task.rb

class Task < ActiveRecord::Base
  has_many :projects, through: :project_task
end

的routes.rb

...
resources :projects do
  member do
    get 'affect_task/:task_id', action: 'affect_task', as: :affect_task                                     
  end
end

项目/ show.haml

= link_to "task_name", affect_task_project_path(task_id: @task_id, project_id: @project_id) 

controller.rb

...
def affect_task
...
  CollaboratorTask.create(task_id: params[:task_id], project_id: params[:project_id])      
...
end
...

当然这是一个例子,所以你理解..