Ruby on Rails
Sunday, June 28, 2015
Jeez.... For a first time effort, you have done an excellent job... Seems that you have great instincts..
I don't see your form here, but defs rankup an rankdown should be consolidated into a controller action 'update'. What's the name of your view? Seems here it should be called list.
If I were you, I would start from scratch... Seems to me that you didn't use Rails generators in your process.
Start by creating a new rails app
Once that loads properly, go to your GemFile and add:
gem "nifty-generators", :group => :development ## https://github.com/ryanb/nifty-generators
run bundle install
Hopefully that worked....
{For some reason I can't expand my text here, ending abruptly}
On Sunday, June 28, 2015 at 2:51:57 PM UTC-4, Federicko wrote:
-- I don't see your form here, but defs rankup an rankdown should be consolidated into a controller action 'update'. What's the name of your view? Seems here it should be called list.
If I were you, I would start from scratch... Seems to me that you didn't use Rails generators in your process.
Start by creating a new rails app
Once that loads properly, go to your GemFile and add:
gem "nifty-generators", :group => :development ## https://github.com/ryanb/nifty-generators
run bundle install
Hopefully that worked....
{For some reason I can't expand my text here, ending abruptly}
On Sunday, June 28, 2015 at 2:51:57 PM UTC-4, Federicko wrote:
Hi All,I am learning rails at the moment and have gone through one of the tutorials on the rails website for creating a simple blog system.I have added some new features to it and it is working great.However, I would like to show someone my code and see if it is the right or most efficient way of achieve this.This system is based on the blog system from the Getting Started with Rails guide which can be found on http://guides.rubyonrails.org/getting_started.html I simply added a rank up/rank down function to the blog system:First, in my routes.rb I added:
resources :articles doresources :commentsmember doget 'rankup'get 'rankdown'endend
Then, in my controller I added two new actions:
def rankup@this_article = Article.find(params[:id])@new_rank = @this_article.rank.to_i-1@prev_article = Article.find_by(rank: @new_rank)@prev_article.rank = @this_article.rank@this_article.rank = @new_rank@this_article.save@prev_article.saveredirect_to articles_pathenddef rankdown@this_article = Article.find(params[:id])
@new_rank = @this_article.rank.to_i+1@next_article = Article.find_by(rank: @new_rank)@next_article.rank = @this_article.rank@this_article.rank = @new_rank
@this_article.save@next_article.saveredirect_to articles_pathend
I also updated the destroy action to include a re ranking function:
def destroy@article = Article.find(params[:id])@start_rank = @article.rank@next_articles = Article.where(["rank > ?", @start_rank]).order('rank ASC')
@next_articles.each do |article|article.rank = @start_rankarticle.save@start_rank = @start_rank + 1
end@article.destroyredirect_to articles_pathend
And in the view I simply added the links to the list:
<% @articles.each.with_index do |article, index| %><tr><td><%= article.title %></td><td><%= article.text %></td><td><%= article.rank %></td><td><%= link_to 'View', article_path(article) %></td><td><%= link_to 'Edit', edit_article_path(article) %></td><td><%= link_to 'Delete', article_path(article), method: :delete, data: {confirm: 'Are you sure?'} %></td><td><% if index != 0 %><%= link_to 'Up', rankup_article_path(article) %><% end %></td><td><% if index != @articles.count-1 %><%= link_to 'Down', rankdown_article_path(article) %><% end %></td></tr><% end %>
As mentioned, I am new to RoR so I don't know if I'm doing this correctly according the Rails convention but the code is working great so I'm happy about that.If someone can review my code please and tell me what I can improve on, that would be great.I'm also thinking there might be an existing gem or something that I can install that will do the ranking for me automatically.Anyway, look forward to your feedbacks.Thanks in advance.
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/348bdaf3-8f9d-411a-a18d-b60b4b341320%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment