Rails 5 - Using Polymorphic Associations - Rendering the Views

Ruby on Rails. Views in polymorphic associations

Add local variable:commentable => @article, while rendering the commentaries form

<%= render 'commentaries/form', :commentable => @article %>

Access the local variable from you partial view views/commentaries/_form.html.erb

<% commentable.commentaries.each do |comment| %>
...
<% end %>
...
<% form_for [commentable, Comment.new] do |form| %>
...
<% end %>

How to get associated polymorphic objects in rails 5?

I'm not sure, but I think that AR doesn't provide a way to load polymorphic associations in just one query. But you can use:

post = Post.find(1)
post_followers = post.followers.includes(:owner)
post_owners = post_followers.map(&:owner)

Rails: includes with polymorphic association

This will hopefully be fixed in rails 5.0. There is already an issue and a pull request for it.

https://github.com/rails/rails/pull/17479

https://github.com/rails/rails/issues/8005

I have forked rails and applied the patch to 4.2-stable and it works for me. Feel free to use my fork, even though I cannot guarantee to sync with upstream on a regular basis.

https://github.com/ttosch/rails/tree/4-2-stable

RailsAdmin: polymorphic has_one association rendering in edit and list views

There's an option for that:

active true

About your second question, you may want to use the configure verb instead of field.

configure won't interfere with the flow of default fields like field does.

RailsAdmin has a project wiki. Whenever you find something that's not documented, you can add it there.



Related Topics



Leave a reply



Submit