haml pull-right as a second class not working when followed by an if - haml

So I have this piece of .erb code I want to translate to .haml. (some says haml is faster, I think i'm still strugling within the learning curve ;) )
<div class="row">
<div class="col-md-12">
<ul class="list-inline pull-right">
<% if current_page?(webservices_path) %>
<li class="submenu_links_current">
<% else %>
<li class="submenu_links">
<% end %>
<%= link_to webservices_path do %>
<i class="fa fa-home" aria-hidden="true" style="color: white;"></i>
Webservices
<% end %>
</li>
</ul>
</div>
</div>
The nearest haml translation I have atm :
.row
.col-md-12
%ul.list-inline.pull-right
- if current_page?(webservices_path)
%li.submenu_links_current
= link_to webservices_path do
%i.fa.fa-home{"aria-hidden" => "true", "style" => "color: white;"}
Webservices
- else
%li.submenu_links
= link_to webservices_path do
%i.fa.fa-home{"aria-hidden" => "true", "style" => "color: white;"}
Webservices
I have two problems with this haml right now.
.pull-right is not applied to ul if there's an if statement between ul and li.
I also found that this following more factorized code doesn't work since while currently on webservices_path, link_to webservices_path is not displayed.
.row
.col-md-12
%ul.list-inline.pull-right
- if current_page?(webservices_path)
%li.submenu_links_current
- else
%li.submenu_links
= link_to webservices_path do
%i.fa.fa-home{"aria-hidden" => "true", "style" => "color: white;"}
Webservices
Thanks !

I think you want something like this:
.row
.col-md-12
%ul.list-inline.pull-right
%li{:class => (current_page?(webservices_path) ? 'submenu_links_current' : 'submenu_links')}
= link_to webservices_path do
%i.fa.fa-home{"aria-hidden" => "true", "style" => "color: white;"}
Webservices
Note the two li have been combined into a single element, setting the class using the class element of the hash rather than the dot syntax. This removes the duplication and of the link_to. Also it is indented under the ul (in your code the if statements are at the same level, so the lis won’t be children of it).

Related

Adding menus in Rails 3

A bit of issue here. Im trying to add a menu to all pages. Reason for this is the ease of editing a single file which updates all web pages.
In my layouts/application.html.erb I have this, between body tags:
<% content_for :menu do %>
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
<% end %>
<%= yield %>
And in my welcome/index I have:
<div id="menu">
<%= yield :menu%>
</div>
<h1>Welcome to my index page!</h1>
Not sure if all that is needed so when I go to my root, I only see what is in the welcome/index file and not the links. Am I missing something?
Its should be other way around, that means you could call the :yield in application layout and have the content_for in your index file.
Actually the idea of the content_for tags are to allow slightly different variations for different pages but, still calling the same name from layout. read more about content_for from here
And I think , in your case what you need is a partial in the layout, or even you can have your menu in the layout itself. since the layout is visible for every page your menu will be available for each page, and if you need modifications in later, still you have to change only one page
1 - as a partial
#app/views/layouts/_menu.html.erb
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
#app/views/layouts/application.html.erb
<%= render partial: 'menu' %>
<%= yield %>
2 - having all in the same file
#app/views/layouts/application.html.erb
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
<%= yield %>
you should have the <%= yield :menu%> in your application.html file.
Also you can make a folder (lets name it "shared") where you can have your menus, messages etc.
Then you can try something like this
#welcome/index.html.erb
<div class="content">
Your Index Content here.
</div>
#layouts/application.html.erb
<%= yield :menu %>
<%= yield %>
#shared/menu.html.erb
<% content_for :menu do %>
<div id="menu">
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
</div>
<% end %>

Articles appear too many times

I started using Ruby on Rails a few days ago, and I have a problem with the each do loops.
Here's my AccueilController's code :
def index
#postsTest = Post.last(1)
#articles = Article.last(1)
#articlesList = Article.last(2)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #users }
end
Here's my application.html.erb code :
<div id="container">
<div id="col1">
<%= render :partial => 'shared/listArticles', :collection => #articlesList %>
<div class="clear"></div>
</div>
<div id="col2">
<%= yield %>
</div>
</div>
</div>
And now my shared/listArticles's code :
<% #articlesList.each do |article| %>
<div id="blocArticle">
<div id="pic">
<%= image_tag (article.photo.url(:thumb)) %>
</div>
<div id="contentArticle">
<div id="titleArticle">
<%= link_to article.title, article %>
<div class="clear"></div>
</div>
<div id="contentArticleDesc">
<%= link_to article.desc, article %>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<% end %>
And my problem is that if I write #articlesList = Article.last(2) then the last two articles will appear two times; if I write #articlesList = Article.last(3) then the last three articles will appear three times etc...
Of course, I would like that each of them appear just one time.
Does someone have any ideas about the source of the problem ?
You're rendering a partial with a collection, therefore Rails calls the partial for each item in the collection. Remove your loop from the partial view or remove the collection param, don't do both!

Rails 3.2 using content_tag to generate a 'Delete' button with twitter-bootstrap icons

I'm trying to replicate the Delete button icon in this example using the Rails 3 content_tag method, within a nested form and using jQuery unobtrusively (or at least I hope to be).
Twitter-Bootstrap Delete Button icon (example)
The html produced in when inspecting with Firebug is below.
<a class="btn btn-danger" href="#">
<i class="icon-trash icon-white"></i>
Delete
</a>
I'm using the following to generate the button with an icon but cannot add the "Delete Ingredients" to it, nor do I get the "#" for the href.
Here is my code from within the ingredients partial:
<%= link_to content_tag(:a, content_tag(:i, '', class: "icon-trash icon-white"), class: "btn btn-danger remove_fields") %>
This generates:
<a class="btn btn-danger remove_fields">
<i class=icon-trash icon-white"></i>
</a>
This was based on information from
Api dock - content_tag
which had the following code example:
content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong")
# => <div class="strong"><p>Hello world!</p></div>
Can someone kindly point me in the right direction? Why am I missing details I mentioned above?
N.B. I can get this to work with a link_to block but wanted to know if this can be done in one line without the do..end and more importantly in a content_for method.
<%= link_to('#', class: "btn btn-danger remove_fields") do %>
<i class: "icon-trash icon-white"></i>
Delete
<% end %>
<%= link_to(body, url, html_options = {}) %>
So your request in 1 line is:
<%= link_to content_tag(:i, "", class: "icon-trash icon-white") + "Delete", path_to_stuff, method: :delete, class: "btn btn-danger remove_fields" %>
The most complicated part is the 'body'. You just have to remember that all these content_tag helpers (including link_to and others), return a string.
BUT, this is ugly. AND long. AND hard to maintain. So your proposed solution that takes the block is much better.
why not just make a view helper method
def link_to_delete
link_to %{<i class="icon-trash icon-white"></i> Delete}, '#', class: "btn btn-danger remove_fields"
end
then call it in you views with link_to_delete
although why do you need your it to be a link
def delete_button
%{<span class="btn btn-danger remove_fields"><i class="icon-trash icon-white"></i> Delete</span>}
end
i dont like it as much with content_for but:
content_tag(:span, %{#{content_tag(:i, nil, :class => "icon-trash icon-white")} Delete}.html_safe, :class => "btn btn-danger remove_fields")
if you want it with an a tag just change :span to :a

Hide Page Elements From View

I have a blog set up with the usual articles and tags on the index page. The Tags section also shows up on the sign-in and sign-up pages which doesn't look great.
The tags are set up in their own column under _side.html.erb - is there a way to hide them from all other pages apart from the index page?
<h3>Blog Tags</h3>
<div id= "tags" >
<% cache('all_tags') do %>
<% for tag in Tag.find(:all, :order => 'name') %>
<ul style="list-style-type: none">
<li>
<%= link_to "#{tag.name}", tag_path(tag) %>
</li>
</ul>
<% end %>
</div>
If you
render :partial => 'side'
from within your application.html.erb, then you could use another layout for the index page which then renders the partial while the standard layout would not.

Render partial with content input in rails 3

I am trying to DRY up some of my HTML in my application currently I have a block of HTML that will get re-used multiple times
<div class="block">
<div class="block_head">
<div class="bheadl"></div>
<div class="bheadr"></div>
<h2>Configuration Needed</h2>
</div>
<div class="block_content">
<div class="message warning">
<p>You have not create an admin user yet</p>
</div>
</div>
<div class="bendl"></div>
<div class="bendr"></div>
</div>
What I would like to do is to create a partial or something along those lines and be able to pass in the content to the block header and content
Does anyone know of a way to do this in rails 3
The way i do it is to have a views/shared folder. Then, i create partials inside and i call them like :
<%= render "shared/flash_error", :error => flash[:error] %>
where shared/flash_error is :
<% if error %>
<%= error %>
<% end %>
If you want to have your partials in the partial folder, use this syntax :
<%= render :partial => "partials/your_partial", :locals => { :error => flash[:error] } %>