Skip to main content

Join GoRails to continue learning

Subscribe to GoRails to get access to this episode and all other pro episodes, and new awesome content every month.

Subscribe Now
Only $19/month

Login to your account

19 How To Build A Forum:

Forum Series Part 4: div_for

Episode 28 · October 16, 2014

The div_for method gives you easy access to creating id's and classes for your ActiveRecord objects in the views. It also makes for easy access to elements with CSS and Javascript.

Frontend Views


Transcripts

Subscribe or login to view the transcript for this episode.

Discussion


Fallback

Another great video!


Fallback

In diving into div_for, I just discovered the ActionView method dom_id which will turn an instance of ForumPost into something like forum_post_10. It's what Rails uses behind the scenes for generating the div id in div_for. Before that (if I had to do it manually) I would have done something like "forum_post_#{@forum_post.id}"

Fallback

Oh sweet. I remember seeing dom_id before but absolutely forgot about it!

Fallback

There's also dom_class


Fallback
Fritz Rodriguez Jr.

Great video, really useful..thanks!


Fallback

Chris, how can you use the anchor tag with pagination? For instance you use anchor tag <%= link_to posts_path(anchor: post-#{post.id}) that points to a record which can be found on the 2nd or 3rd page of the posts_path thanks to the pagination. In this case the link_to helper will take you to the top of the posts_path instead of the record since the div tag the anchor tag is looking for can't be found in the DOM.


Fallback

div_for has now been removed from Rails

Fallback
Владислав Коваленко

But if you really want to use it just install 'record_tag_helper' gem


Fallback

Getting this error Base-level rules cannot contain the parent-selector-referencing character '&'. When adding
&:target {
background-color: rgb(248,238,199);
padding: 8px;
}


Login or create an account to join the conversation.