I have project model that has many tasks. Both project and tasks can have many discussions, and therefore I made Discussions polymorphic model(see below).
I wish to be able to click the link and mark ‘discussion’ as finished.
The way I went on to do it is to make a custom action in the ‘discussion controller’ that changes the boolean value of the ‘finished’ attribute from false to true. How to make that link_to helper in the discussion show page succesfully route to that custom action in the discussions controller? Also, is this the best practice to do this?
Discussion model
1 class Discussion < ActiveRecord::Base
4 belongs_to :user
5 belongs_to :discussionable, :polymorphic => true
28 end
Project model
1 class Project < ActiveRecord::Base
7 has_many :tasks, :dependent => :destroy
8 has_many :discussions, :as => :discussionable, :dependent => :destroy
24 end
Task model
1 class Task < ActiveRecord::Base
7 belongs_to :project
14 has_many :discussions, :as => :discussionable, :dependent => :destroy
27 end
So far my link_to helper looks like below, but it doesnt work(doesnt strike the custom ‘finish’ action as I want)…
Discussion show
7 <%= link_to 'Finish discussion', polymorphic_path([@parent, @discussion]), :action => 'finish' %>
This is this custom finish action in discussion controller.(I have before_filter that defines this @discussion variable from params[:id])
33 def finish
34 if @discussion.update_attribute(:finished, true)
35 flash[:notice] = "it worked"
36 else
37 flash[:alert] = 'You must be an admin to do that'
38 end
39 end
I also haven’t fiddled with routes.rb, as I don’t know if I have to.
Routes.rb
1 PrjctMngr::Application.routes.draw do
13
14 # PROJECTS
15 resources :projects do
16 resources :tasks
17 resources :discussions
18 end
19
20 # TASKS
21 resources :tasks do
22 resources :subtasks
23 resources :discussions
24 end
31
32 # DISCUSSIONS
33 resources :discussions do
34 resources :comments
35 end
36
37 end
action option is for the path helper, not tag helper 😉