Ruby on Rails
Monday, June 29, 2015
Hi All,
I have moved everything over to the model as suggested. Check it out :p
In the model, I created a Class method as well as the previous instance methods:
class Article < ActiveRecord::Base
def self.rank(id, rank) @this_article = find(id)
if (rank == "up") @next_article = find_by(rank: @this_article.rank.to_i-1)
@this_article.rankup @next_article.rankdown else @next_article = find_by(rank: @this_article.rank.to_i+1)
@this_article.rankdown @next_article.rankup endend
def rankup self.rank = self.rank - 1 self.saveend
def rankdown self.rank = self.rank + 1 self.saveend
end
And now in my controller, I only have one action:
def rank Article.rank(params[:id], params[:rank]) redirect_to articles_pathend
However, can anyone please review the code and let me know if it is correct in terms of OOP?
And if it is best practice for Rails.
Rails is awesome.
Cheers
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d9630b45-6824-4ab6-9f11-62b677df039f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment