I’m looking to clean up my controller, as it seems heavy and redundant. Any help on how I’d go about moving this type of logic into my model would be appreciated. Thanks for any help on this – the code below is my for index action:
case params[:find_by]
when 'topic'
nuggets = Nugget.where(['topic = ?', params[:topic_name]])
@nuggets = nuggets.paginate(:page => params[:page],:per_page => 15)
@title = nuggets.first.topic
when 'audience'
nuggets = Nugget.where(['audience = ?', params[:audience_name]])
@nuggets = nuggets.paginate(:page => params[:page], :per_page => 15)
@title = nuggets.first.audience
else
@nuggets = Nugget.paginate(:page => params[:page], :per_page => 15)
end
I’m not entirely sure I would move it into the model. I’d probably just move it into a private utility method in the controller.
Another option would be to create routes for the different finds; whether or not it’s worth it, meh. You could move the find_by logic into the model, or use a
sendto slightly DRY up the topic/audience difference, but again, that seems more trouble than it’s worth.I’ll be interested to see what more Rails-y people think about the question, though.