How to specify twitter bootstrap's <i> tag in Embedded Ruby - ruby-on-rails-3

-Newbie to Ruby on Rails and integrating a few aspects of Twitter Bootstrap in (or at least trying :))-
I have a link in regular HTML with Twitter Bootstrap icons in it:
<li class="sidebar"><i class="icon-user"></i> Home</li>
(The i tag is for twitter's image icons)
If I were writing that same HTML in embedded Ruby, how could I still get the icon in there?
Right now I have:
<%= link_to content_tag(:li, "Home"), root_path %>
Is it possible to specify Twitter's tag in embedded ruby?
Also, should I specify the sidebar class in regular html or with :class => "sidebar"
Thanks!

Sure, just try something like:
<%= content_tag(:li, :class => 'sidebar') do %>
<%= link_to '#' do %>
<%= content_tag :i, '', :class => 'icon-user' %> Home
<% end -%>
<% end -%>

The shorter alternative I use is
link_to " ".html_safe, '#'
It's 'dirtier' but somewhat easier to understand.

Related

Use icomoon with link_to and icon_tag

I'm trying to move from Font Awesome to icomoon.
With Icomoon I can get the icons to work using the following syntax
<span data-icon="" aria-hidden="true"></span>Some Text
However, as I'm using a rails app I'd really prefer to use the following syntax, or something similar.
<%= link_to icon_tag("icon-pdf", "some text"), controller_path %>
I have tried the following as well, all to no avail
<%= link_to 'Some text', controller_path, {"data-icon" => "", "aria-hidden" => "true"} %>
It doesn't matter if I put the defined name (icon-pdf) or its hex value in there, but I can't seem to get the icon to appear.
Is there a way I can achieve this, or am I stuck with the data-icon method?
Try with this:
<%= link_to controller_path do %>
<span data-icon="" aria-hidden="true">Some Text</span>
<% end %>
Or
<%= link_to content_tag(:span, 'Some Text', :data_icon => "", :aria-hidden => "true" ), controller_path %>
It should work. Thanks

tinymce issue with rails 3

I have legacy rails 3 app, where I need to modify a page to use tinymce to edit a text_area.
There are existing pages in this app that already use tinymce.
For reasons I cannot go into here, I cannot use any of the tinymce plugins that are available.
Now my problem is as follows.
I have a model called Sections, that has two attributes, section_name and html.
I want to be able to edit the html using tinymce.
My view has a form which is as follows
<%= form_for #section , :url => update_section_path , :method => :put do |f| %>
<%= f.hidden_field :id %>
<%= f.label :section_name , "Section Name" %>
<%= f.text_field :section_name %> <br />
<%= f.label :html, "Html" %>
<%= f.text_area :html %>
<%= f.submit "Update" %>
<% end %>
The form appears as expected.
The TinyMCE editor also appears on the page with the original html.
The problem is that when I click on the Update button, the put query sent to my server, does not contain the new modified content of the Html text_area. It sends back the original text that was in that text_area.
Could anyone help me understand why.
Thanks in advance
=Puneet
I found an issue in my html which was causing this. Once I fixed that issue it worked fine

Why is my nested text_area helper adding html tags?

I have a text_area in a partial in a complex form that is called like so
<%= f.fields_for :notes do |notes_form| %>
<%= render :partial => 'note', :locals => {:f => notes_form, :operation => f, :count => operation.notes.count} %>
<% end %>
<p><%= add_child_link "Add note", :operation_notes %></p>
and the partial looks like this
<% count ||= 2 %>
<div class='fields'>
<%= f.text_area :note_text, :rows => "4", :class => "notes" %>
<%= remove_child_link "x", f, count %>
</div>
There can be many notes on the form hence the add and remove child links.
The issue I'm having is that if I add a note with the text 'abcd', when I bring up the edit form I get '<p>abcd</p>'. If there are line breaks in the note it adds <br /> tags. The text_area form helper seems to be using the simple_format helper but I have no idea why. Can anyone help as this is very undesirable behaviour?
Ah solved,
Earlier on the same page I was displaying the note and using simple_format to format it with
<%= simple_format note.note_text %>
It seems that simple_format is somewhat destructive as after this, a call to note.note_text always returns the formatted text. If I change the above to
<%= simple_format note.note_text.dup %>
then the note_text attribute is not altered and I get the appropriate results.
I will have to look more closely at simple_format but this really strikes me as undesirable behaviour.
EDIT
It looks like this has been corrected in Rails 3.1
I would suspect that you have something in your Note model that is processing the text. Check for callbacks in this model.

Redirecting outside a web app?

This is my first time developing a rails app from scratch. The goal of my code is to use a title and link (both stored in a database table) to redirect users to the link. (Problem) When I click the link title, I'm redirected to localhost:3000/google.com instead of google.com. (Assuming google.com was the value in link.link)
<h1>Links#index</h1>
<% #links.each do |link| %>
<p>
<%= link_to link.title, link.link %>
</p>
<% end %>
Notes:
(1) Using Rails 3.1
(2) The contents of my routes.rb file are below (Not sure if the use of resources :links has something to do with my problem)
CodeHed::Application.routes.draw do
resources :links
get "links/index"
root :to => "links#index"
end
Are your links prefixed with "http://"? If not, try adding that in programmatically with something like:
def add_http(link)
if (link =~ /http(?:s)?:\/\//)
link
else
"http://#{link}"
end
end
If that doesn't work then you could simply enter raw html:
<h1>Links#index</h1>
<% #links.each do |link| %>
<p>
<%= link_to title, add_http(link) %>
</p>
<% end %>
(I haven't checked this code)

Is it possible to use partials to be rendered as wrappers in Rails?

I would like to render structures like this:
<tag1>
<tag2 someattribute="somevalue">
<.. lot of things inside ..>
</tag2>
</tag1>
<tag1>
<tag2 someattribute="someothervalue">
<.. different inside things inside ..>
</tag2>
</tag1>
The tag1, tag2 are the same, they are just parametrized. The inner part of the code changes. I tried to implement the thing above like that (haml):
%div{id:['products', id]}
.products_content
%div{id:['products', id, 'content'], class:'products_mask'}
= yield
This was the partial _content_head.html.haml, which is called from a template:
= render 'shared/content_head', id: 'all' do
%h3= Title
%p= Body of the text.
My theory that yield inside the partial would lead to rendering of the passed block did not prove. Is there a way to use partials as code wrappers? Can you suggest me some solution how to reach this? Thank you.
This might be a good use of the capture method.
I'm only familiar with ERB, but here is the general idea:
<% structure = capture do %>
<h3>Title</h3>
<p>Body of text</p>
<% end %>
Then pass the variable into the partial:
<%= render 'shared/content_head', :structure => structure %>
And within the partial, spit out the structure variable:
<%= structure %>
Reset structure multiple times within the view as you render partials (or maybe more appropriately, in a helper?).
I've used the following (Rails 4, but I think it should work with Rails 3 too):
<%# app/views/users/_edit.html.erb %>
<%= render layout: 'modal_wrapping' do |f| %>
<%= f.input :email %>
...
<% end %>
.
<%# app/views/users/_modal_wrapping.html.erb %>
<div id='modal'>
<%= simple_form_for #user do |f| %>
<%= yield f %>
<% end %>
</div>