Using tiny_mce from https://github.com/kete/tiny_mce
to be able to change the format of the text, the problem is once its submitted it sends the html to my comments and its not getting translated and just outputs to plain html shown below
<ul> <li><span style="text-decoration: underline;"><strong>hello </strong></span></li> <li><span style="text-decoration: underline;">test</span></li> <li><span style="text-decoration: underline;">est</span></li> <li><span style="text-decoration: underline;">est<br /></span></li> </ul>
How do i get rails to translate the html so it displays boldness etc..
I've tried putting it in HTML tags <html><%= comment.body %></html> which does not work!
Use the raw helper:
<%=raw comment.body %>
You should sanitize the input in your model using a before_validation filter.
I like gem 'sanitize', with it you can self.body = Sanitize.clean( self.body, Sanitize::Config::RESTRICTED )
Then you can safely use <%= raw comment.body %> or <%= comment.body.html_safe %> to display the HTML.
Related
I am listing previously used shipping addresses for user to select. I`m dynamically adding classes
<div class="row">
<% #shipping_addresses.each do |address| %>
<ul class=<%= "shipping_address_#{address.id}" %> >
<li><%= address['name'] %> </li>
<li><%= address['street'] %> </li>
<li><%= address['city'] %></li>
<li><%= address['country'] %></li>
<li><%= address['zip'] %></li>
<li><%= address['phone'] %> </li>
</ul>
<% end %>
</div> <!-- row -->
Problem is, that now I am trying to add a col-lg-3 bootstrap class to my ul`s, and it doesn`t work when I write it like this:
<ul class=<%= "shipping_address_#{address.id} col-lg-3" %> >
I get this output:
<ul class="shipping_address_38" col-lg-3="">
I also tried several different options and they don`t work.
Can anyone help?
Thank you
The actual html you are outputting is
<ul class=shipping_address_38 col-lg-3>
And your browser is interpreting this as best it can. The quotation marks in your template never make it to the actual html because they're not actually part of the string.
You could do something like
<ul class="<%= "shipping_address_#{address.id} col-lg-3" %>" >
Although in my opinion you're now past the point where ERB gets difficult to read - you may wish to refactor this into a helper.
I would like for my app to allow users to post links in their posts, and for those links to automatically be recognized. To do so, I have been using auto_link as such: the following is the partial that is called to show a person's post:
_post.html.erb:
<tr>
<td >
<span class="post_header"><h4><%= link_to "#{post.user.first_name} #{post.user.last_name}", post.user %></h4></span>
<p> <%= auto_link(post.content) %> </p>
<span class="post_timestamp">
Opined <%= time_ago_in_words(post.created_at) %> ago
</span>
</td>
</tr>
this outputs the following, for a single post.content:
<p> Wondering if this link <a href="http://www.economist.com/blogs/freeexchange">http://www.economist.com/blogs/freeexchange</a> will become a proper link
Why does auto-link create/escape the angle brackets to <a etc? Is there some way to fix this, as this does not create working links. Instead the output in the browser is:
Wondering if this link http://www.economist.com/blogs/freeexchange will become a proper link
In Rails 3, erb will default to not allow any ruby output to contain html. To get around this you can use "some string".html_safe
<%= auto_link(post.content).html_safe %>
But of course any html or javascript will then be allowed. So...
<%= sanitize(auto_link(post.content).html_safe) %>
Note that auto_link was removed with Rails 3.1.
See this answer for replacement solutions.
tybro0103's solution works, but if you want ONLY links as proper HTML, you'll need
sanitize(auto_link(post.content).html_safe,tags:'a')
I am trying to use the tabs function from jQuery UI in a Rails app. I am using a helper for the navigation and I would like to keep it that way. The code in my helper is:
def links_for_navigation
html = ""
html = <<HTML
<ul>
<li>Courses</li>
<li>Parts</li>
<li>Categories</li>
</ul>
<div id="tabs-1"><% link_to "Courses", courses_path %>
<div id="tabs-2"><% link_to "Parts", parts_path %>
<div id="tabs-3"><% link_to "Categories", categories_path %>
HTML
end
My view pulls in the code with <% links_for_navigation %>
I added to my application.js:
jQuery(function() {
jQuery("#tabs").tabs();
});
And my application.html.erb has:
<%= stylesheet_link_tag 'courses', 'jquery-ui-1.8.13.custom.css' %>
<%= javascript_include_tag :defaults %>
<%= javascript_include_tag 'jquery-1.5.1.min.js', 'jquery-ui-1.8.13.custom.min.js', 'application' %>
When I try to load the page I get cannot find string HTML before EOF. What am I doing wrong?
I recommend using a partial, rather than a helper method. The partial, I'll call it _nav.html.erb, would look like the following (note the id="tabs" on the surrounding div):
<div id="tabs">
<ul>
<li>
Courses
</li>
<li>
Parts
</li>
<li>
Categories
</li>
</ul>
<div id="tabs-1">
<%= link_to "Courses", courses_path %>
</div>
<div id="tabs-2">
<%= link_to "Parts", parts_path %>
</div>
<div id="tabs-3">
<%= link_to "Categories", categories_path %>
</div>
</div>
Then in the appropriate view file, you can insert the partial with a call to render:
<%= render :partial => 'nav' %>
I've never used jQuery tabs but try adding this to application.js, based on this example. The "#tabs" selector inside $() corresponds to the id="tabs" from above:
$(function() {
$( "#tabs" ).tabs();
});
I'll offer a couple tips/reminders as well:
Embedded ruby (ERB) tags needs to have an = if you want them to print something, i.e. <%= #user.name %> versus <% #user.name %>. The first will output a string into the file, the second will not. There are cases where you might not want to print something, so you would leave off the =.
Be sure to close all your HTML tags. Rails' ERB files can get messy anyways, so proper HTML structure is key.
Avoid using HTML outside of *.html.erb files. This isn't a golden rule, but it's a good rule of thumb.
how do i write a Logo thats made out of pure css so its just like how i would write it in its html form, how does it look in Ruby/Ruby on Rails?
Right now i have this:
<div id="logo">
<h1><%= link_to 'Title <span>title</span>'.html_safe, root_path %></h1>
</div>
I am new to Ruby and Rails so i have no idea how to include the < span > and i don't think i should be playing with the .Html_safe either.
In cases like this, I prefer to have more control over the HTML, since including HTML as a string to a helper method is just nasty.
<div id="logo">
<h1>
<a href="<%= root_path %>">
Title <span>title</span>
</a>
</h1>
</div>
The helpers like link_to are helpful, but when they make you jump through hoops they aren't worth using.
<div id="logo">
<h1><%= link_to raw('Title <span>title</span>'), root_path %></h1>
</div>
You can use link_to with a block:
<%= link_to root_path do %>
Title <span>title</span>
<% end %>
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">
Link-1
</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!
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">
Link-1
</div>
<% end %>
Edit: Ryan Bigg is right. Rails doesn't output a <p> tag. Sorry for the wrong info.
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>