views:

51

answers:

2

I'm new to Ruby and Rails and I have a simple controller that shows an item from the database in a default view. When it is displaying in HTML it is outputting <p> tags along with the text content. Is there a way to prevent this from happening? I suppose if there isn't, is there at least a way to set the default css class for the same output in a statement such as this:

<% @Items.each do |i| %>

    <%= i.itemname %>
    <div class="menu_body">
           <a href="#">Link-1</a>
           </div>
<% end %>

So the problem is with the <%= i.itemname %> part. Is there a way to stop it from wrapping it in its own <p> tags? Or set the css class for the output?

Thanks!

A: 

You canchange the public/stylesheets/scaffold.css if you want.

Or if you want to change it for a single page say items/index.html.erb

<style> 
p{
/* your style here *?

}

</style> 
Salil
He said it's outputting p tags when he doesn't want it to; he didn't mention stylesheets at all.
Benson
+2  A: 

You need to enclose it with the HTML tag of your choice. Also if required you can escape bad code by using <%=h i.itemname %> Example:

<% @Items.each do |i| %>

    <div><%=h i.itemname %></div>
    <div class="menu_body">
           <a href="#">Link-1</a>
           </div>
<% end %>

Edit: Ryan Bigg is right. Rails doesn't output a <p> tag. Sorry for the wrong info.

Shripad K
That's awesome, I didn't even realize RoR added paragraph elements if you didn't wrap your stuff properly. Thanks!
Benson
Thank you! Good lord, it somehow stored it in the database with P tags in the first place. Sorry, but thanks for the info, I learned something anyway!
dt
You are welcome :)
Shripad K
Aha! I figured out your prob at last! The `<p>` tags are being added simply because you weren't escaping your output. Thats why you use `<%=h i.itemname %>` most of the times and not just `<%= i.itemname %>` Say even if you added a div tag around `<%= i.itemname %>` it will still output a `<p>` tag if you have content with `<p>` tag stored in the database! Hope that clarifies your issue with rails outputting `<p>` tags! Cheers! :) (Figured out when you said `it somehow stored it in the database with P tags in the first place`)
Shripad K