How to include an id (or class) in an ERB element - html

I have this code for displaying comments on an article which uses ERB elements to fetch and display information from a database:
<div class="comments">
<h4 class="comment_author">Comment by <%= comment.author_name %></h4>
<p class="comment"><%= comment.body %></p>
</div>
I would like to format the comment.author_name differently, so I wanted to include an id on the second line e.g.
<h4 class="comment_author">Comment by <%= comment.author_name, id: "commenter" %></h4>
However, when I do this I get the following error message.
I have added classes and ids in the past to form elements with the
:class => "name" syntax like so:
<%= f.submit 'Create Article', :class => 'create_new_article_button'%>
But that doesn't seem to be working here either. Is there a different way to add ids and classes to non-form ERB elements?
Thanks in advance!

<%= comment.author_name %> is not a tag/element, its just a value(string i assume) & hence you cannot add an id attribute to it. You could instead do
<h4 class="comment_author">Comment by
<span id="commenter"><%= comment.author_name %></span>
</h4>
Or give the id="commenter" directly to the <h4> tag & use that to target the span.
It worked earlier because f.submit refers to a submit button of a form, meaning it was actually an HTML element & hence you could add an id or a class to it.

You can not add an ID to a content. Instead you should add ID to the element which is wrapping your content. In your case it should be like this
<h4 class="comment_author" id='commenter'>Comment by <%= comment.author_name %></h4>
This will create h4 element with an ID 'commenter'.

You are confusing here
I have added classes and ids in the past to form elements with the
:class => "name" syntax like so:
<%= f.submit 'Create Article', :class => 'create_new_article_button'%>
But that doesn't seem to be working here either.
Form elements will receive class has one of it's arguments. Check the docs here . But <%= %> will just display the end result of ruby code in the views, but it won't add any html content to the DOM. Please read docs carefully before implementing something.

Related

Remove <a> tag from rails "link_to" methdo

I need to remove <a> tag from rails link_to method. Here is current code and result:
<%= link_to "ESP", :locale=>'es'%>
<a href="/es/blog/crazy_page">ESP</p>
Here is my desired outcome:
/es/blog/crazy_page
Reason I need this is so I could make "alternate" link tag in header for each language. Can't seem to find this anywhere.
Use url_for method
<%= url_for(:locale => 'es') %>

Is there a way to add HTML inside a Rails partial argument?

I have been trying to find something about embedding HTML code inside a partial argument for days but I have not found anything so I'm guessing it isn't possible. But it seems like it should be.
I have a static page in my Rails app which has a lot of sections and each section can have subsections. I could just make the entire page just plain HTML. But I didn't want to repeat the same formatting over and over in case I want to change classes or something else.
So I have the following _section.html.erb partial file:
<div class="row">
<h4><%= heading %></h4>
<% subsections.each do |section| %>
<% if section[:header] %>
<h5 class="primary-text"><%= section[:header] %></h5>
<% end %>
<p><%= section[:body] %>
<% end %>
</div>
That works fine. But what if I want to include a link to a page or an email inside one of the subsections? It doesn't work just by passing it in as part of the quotes text. It shows the actual HTML tags.
Is there a real way to do this or should I give up and just write plain HTML with repeated section formatting?
You mark your text as html_safe. For example:
<%= section[:header].html_safe %>
But I would suggest using sanitize method because of security resonons:
<%= sanitize section[:header] %>
Probably sometimes you will want to configure sanitize method. Here you can read how to do this:
http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html
You can read more about security here:
http://guides.rubyonrails.org/security.html#cross-site-scripting-xss

adding a class to text displayed by rails in the view

very straight forward..
how do I add a class to something like this
<h2><%= guide.title %></h2>
which is just displaying text?
You have to wrap it within some container:
<div class="my_class"><%= guide.title %></div>
The container you'll use depends on the context given text is to be used.
Update:
Since the text is already wrapped in <h2> you can do:
<h2 class='my_class'><%= guide.title %></h2>
Another update:
If you wan to minimize the amount of pure html in your view, you can always do:
<%= content_tag :h2, class: 'my_class' do %>
<%= guide.title %>
<% end %>

link_to does not render dynamic data

My main page content is generated with this code:
<% #post.each do |post| %>
<div class="post" style="background:url(<%= post.postimg.url.to_s%>)center center">
<div class="overlay">
<p class="post_title"><%= post.titre %></p>
<p class="post_pseudo"><%= link_to Utilisateur.find(post.utilisateur_id).pseudo ,Utilisateur.find(post.utilisateur_id).pseudo %></p>
</div>
</div>
<% end %>
This snippet is inn _postgeneration.html.erb. The main page (index.html.erb ) look like this :
<%= render "headerhome"%>
<%= render "postgeneration"%>
When the user is redirected from a controller with redirect_to , everything works as expected, my posts are displayed. But when my page is generated from a link_to, only my HTML is displayed correctly. Here's my syntax for link_to
<%= link_to "homepage", '/home'%>
It looks like link_to refuse to render html elements dynamically generated.
The #post.each do wasn't ignored. The post were generated, but without visual, because the visual was set in inline css. The solution was to delete the inline css and put a image_tag. Now everything is working as expected. I didn't know Rails could ignore inline css. Thanks everyone !

rails form_for styling

Is there a way to add styling to rails form_for and make it display inline?
There might be a cleaner way to do this, but it works. (I tried with another nested hash, no dice)
<% form_for(#model, :html => { :style => 'background-color:red;' }) do |f| %>
A even cleaner way would be to define the styling in an external stylesheet (like application.css). form_for creates a <form id="something"/> tag with an id attribute. You can of course use this id in your stylesheet(s) to apply some specific styling to the form.
Put it in a div of the appropriate class? Its a display thing, not a rails thing.
application.css:
.inline form { display: inline; }
form.html.erb
<div class="inline">
<%= form....
</div>