0

I have a controller with the normal CRUD actions for a resource called "Issue". The Issue table has a boolean called published to save the state of each instance. When an issue gets the published boolean set to true, all the other instances of Issue should be set to false.

My question is about the issues_controller.rb. Right now, I don't have the published boolean listed as attr_accessible. My thought was that I would have a button in my view that routed to a specific action:

def publish
  // before_filter to set all the issues' publish column to false
  // update_attribute to set publish column to true
end

def surpress
  // update_attribute to set this issues publish column to false
end

After doing some research and rethinking my approach, I thought perhaps it would be better to create some sort of new controller - published_issues_controller.rb that uses more resourceful routes:

def create
  // same as the publish method above
end

def destroy
  // same as surpress method above
end

This is my first rails app - any insight on whether one of these approaches (or something completely different) is best would be much appreciated.

jhummel
  • 1,724
  • 14
  • 20

2 Answers2

1

When an issue gets the published boolean set to true, all the other instances of Issue should be set to false

That seems like a strange business requirement to me?

Anyhow, I would use the controller actions you have documented, but would not use the before filter

# routes
resources :issues do
  member do
    post 'publish' # might want put instead of post?
    post 'surpress' 
  end
end

Controller

def publish
  issue = Issue.find(params[:id])
  Issue.transaction do
    issue.publish!
  end
  # ... redirect, etc...
end

Model

def publish!
  published = true
  # set 'All' other issues [published = false]
  # maybe it is only ones that are somehow related to this one? same project?
  save!
end
house9
  • 20,359
  • 8
  • 55
  • 61
0

Yes, include REST with your CRUD (pretty similar actually) and you are there.

With REST you just have resources :issues in config/routes.rb

and then a few standard actions.

The rails guides even list them under CRUD:

http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

So, yes create and destroy are better.

If you're only implementing them, best practice is to use :only => or :except => to limit the routes to them.

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • The problem is I already have create and destroy actions for Issue. Should be creating and destroying some sort of new resource? like a PublishedIssue? - one that is backed by the same DB table as Issue? – jhummel Dec 28 '12 at 03:14
  • Yes, probably with `has_many :through` relationships through it. New table. – Michael Durrant Dec 28 '12 at 03:16
  • I'm not sure I follow, sorry this is all pretty new to me. An issue has a published or unpublished state, only one issue can be published at any one time. How would that new table look, just a foreign key to the issue? Is it a single row that is just updated? – jhummel Dec 28 '12 at 03:32