简体   繁体   中英

rails- Pass infomation to a controller based on url

I have a controller for "Productions" and if you go to localhost/productions you see an index page, you can click show and view the show page for that particular productions. Each production has a unique ID like 036ea872f9011a7c , I want my users to be able to add items to a production like follows:

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

You should build a route with the necessary parameters like this:

Suppose we have tasks that we assign to a project

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

projects/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
...

Of course this is an example so you understand..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM