📜 ⬆️ ⬇️

Polymorphic bonds. Small addition

In December, Habré had a good article about polymorphic links in Rails. Here it is . Before reading further, please read it.
However, the author of the article has several unanswered questions. Here we will find the answers today.


Firstly, there is such code in the text of the article.

  1. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  2. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  3. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  4. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  5. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  6. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  7. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  8. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  9. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  10. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  11. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  12. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  13. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  14. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  15. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end
  16. module PostsHelper def posts_smth_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topic_path ( post ) when "link" : posts_link_path ( post ) when "podcast" : posts_podcast_path ( post ) end end def posts_smths_path ( post ) case post. content . class . to_s . downcase when "topic" : posts_topics_path when "link" : posts_links_path when "podcast" : posts_podcasts_path end end end


It works, of course, but ... this is not a Rails style at all. Everything should be easier. And there is. Rails offer us a great option - the polymorphic_path () method. And also new_polymorphic_path () and edit_polymorphic_path () . The essence of the method, but the fact that it, depending on what models are fed into it, will give us the urls we need. Like this.
')
  1. <% if can? : edit , @post %>
  2. <% = link_to "Edit this #IL@post.content.class.to_s.downcase}" , edit_polymorphic_path ( [ : posts , @post . content ] ) ,: class => "b-post-edit_link g-link-no -visited " %>
  3. <% end %>


Here, depending on what kind of content from @post ( Topic, Link, Podcast ), the url will be substituted for editing it. It is clear that polymorphic_path () and new_polymorphic_path () work in a similar way.

Secondly, the author was suggested to the author in the course of the article that it would be nice to use accepts_nested_attributes_for and even then made an article on this topic, but this is not written in this article with polymorphic connections. And it works very well, the main approach is to know :)

For example, let's look at the controller that will create the Topic.

  1. class Posts :: TopicsController < PostsController
  2. def index
  3. @posts = Post. topics . find ( : all )
  4. end
  5. def new
  6. @topic = Topic. new
  7. @topic . build_post
  8. @selectable_categories = Category. all . collect { | c | [ c. title , c. id ] }
  9. end
  10. def edit
  11. @topic = Topic. find ( params [ : id ] )
  12. @post = @topic . post
  13. @selectable_categories = Category. all . collect { | c | [ c. title , c. id ] }
  14. end
  15. def create
  16. @topic = Topic. new ( params [ : topic ] )
  17. @topic . post . author = current_user
  18. if @topic . save
  19. redirect_to post_path ( @topic. post )
  20. else
  21. @selectable_categories = Category. all . collect { | c | [ c. title , c. id ] }
  22. render : action => "new"
  23. end
  24. end
  25. def update
  26. @topic = Topic. find ( params [ : id ] )
  27. if @topic . update_attributes ( params [ : topic ] )
  28. redirect_to post_path ( @topic. post )
  29. else
  30. @selectable_categories = Category. all . collect { | c | [ c. title , c. id ] }
  31. render : action => "edit"
  32. end
  33. end
  34. end



We see that everything is very easy. And most importantly - it works. The worker prmer of all this can be viewed here .

PS For all those who program on Rail, look at my yesterday's link to Habré . Very interesting things about ActiveRecord in Rails 3.

UPD. Forgot one more little detail. For ease of editing and easy scalability, you can still properly view the view. An example is here (here I don’t want to upload it, because there will be too much code). Following the link is a file with a view for Post , from which, depending on the type of content, the view for Topic or Link is jerked.

Source: https://habr.com/ru/post/81677/


All Articles