HAML and Ruby loop and UL not working - html

I'm trying to get this simple list working, but the ul is closing and not enclosing the li elements in the loop. Am I missing a simple way to do this?
%ul.nav.nav-tabs.nav-stacked
- #courses.each do |c|
%li
= link_to "add", { :controller => "admin/relateds", :action => "edit", :id => c.id }, :confirm => "Are you sure?"
= c.coursetitle

The %li needs indentation because it is within a do block. Even if it is valid markup it will save you debugging time if you opt to use 2 or 4 spaces for indentation for better legibility, as one is very difficult to discern.
%ul.nav.nav-tabs.nav-stacked
- #courses.each do |c|
%li
= link_to "add", { :controller => "admin/relateds", :action => "edit", :id => c.id }, :confirm => "Are you sure?"
= c.coursetitle

you need to indent the %li and what's supposed to be inside. Currently your loop does nothing.

Related

Dropdown list doesn't work as a dropdown (button)

I have this dropdown list:
- if current_user
%li.header__list-item.dropdown
%a.dropdown-toggle{:href => "#", "role" => "button", "data-toggle" => "dropdown", "data-target" => "#"}
Account
%b.caret
%ul.dropdown-menu{"role" => "menu"}
%li
= link_to "Change your password", edit_user_registration_path
%li
= link_to t('devisegeneral.sign_out'), destroy_user_session_path, method: :delete, class: 'header__link header__link--active'
- else
%li.header__list-item
= link_to t('devisegeneral.sign_in'), new_session_path(:user), class: 'header__link header__link--active'
so that when a user clicks in Account, these two links change your password and sign_out should appear. But instead like this I have them both displayed all the time and the "button" account doesn't work. Is there anything I am missing, because I am loosing a lot of time and can't fix this.
I'd recommend using this template instead of yours:
https://www.w3schools.com/CSS/css_dropdowns.asp
I have used it multiple times and It works great!

Assign class to link_to using html slim

I'm working on a html slim file for a Ruby on Rails project. I want to create a button of class btn btn-primary (I'm using bootstrap) for a controller action. Name of controller is default_responses and action is edit. So I first did:
= link_to 'Test this', :controller => 'default_responses', :action => 'edit', :id => params[:id].to_i
This would become
Test this
where 7 is the id parameter and is correct for my case. However, it is not a button at all, just an anchored tag. It also redirects me to the correct page.
Then I tried
= link_to 'Test this', :controller => 'default_responses', :action => 'edit', :id => params[:id].to_i, class: 'btn btn-primary'
which gave me
Test this
This is still not what I want as it is not a button still.
Also tried = link_to 'Test this', :controller => 'default_responses', :action => 'edit', :id => params[:id].to_i, :class=> 'btn btn-primary'
It returned Test this which is still wrong.
Any suggestions? Thanks.
Rails' link_to takes multiple hash options, in your example you're supplying a single hash option, which all get passed into the url_options section. You'll need to add the curly brackets ({}) around the first hash to tell ruby which option goes to which hash.
= link_to 'Test this', { :controller => 'default_responses', :action => 'edit', :id => params[:id].to_i }, class: 'btn btn-primary'
should work. Of course, you could also use a url helper (url_for, default_responses_path, etc) instead of the first hash.

How to set id via data field in rails?

Here is part of my code:
- #search_trees.each do |search_tree|
%ul
= link_to search_tree.getname(search_tree.designation_id), {:controller => "search_trees", :action => "catalog", :cat => search_tree.STR_ID}, :class => 'catalog-click', :id => search_tree.STR_ID
As you can see, i'm setting class and id for link, but how to set id for %ul tag? something like this %ul#=search_tree.STR_ID
I have finded answer by myself (if anybody have better solution, give it please):
%ul{:id => search_tree.STR_ID}

Rails content_tag inserts extra "<" and ">" characters

When doing this:
def user_log
if logged_in? == false
form_tag session_path, :id => "mform" do
content_tag(:span, content_tag(text_field_tag :email, "email#domain.com"), :class => "memail")+
content_tag(:span, content_tag(password_field_tag :password, "12345678912"), :class => "mpass")+
content_tag(:span, content_tag(submit_tag 'Login'), :class => "mbutton")
end
else
...
end
end
end
I get this:
stack overflow doesn't let me post pictures
Since I don't want the extra "<" and ">", what am I doing wrong?
EDIT: As extra information, on my view I am just doing:
<%= user_log %>
The fundamental problem is that you are using content_tag twice when you don't need to. content_tag essentially calls content_tag_string. Here's content_tag_string's source:
def content_tag_string(name, content, options, escape = true)
tag_options = tag_options(options, escape) if options
"<#{name}#{tag_options}>#{content}</#{name}>".html_safe
end
Calling content_tag(text_field_tag :email, "email#domain.com") looks like:
"<#{text_field_tag :email, "email#domain.com"}>"
and text_field_tag already produces a full HTML tag (it includes the "<" and ">").
All you need to do to get rid of the extra angled brackets is to leave out the second content_tag:
content_tag(:span, text_field_tag(:email, "email#domain.com"), :class => "memail")+
Though I haven't tried it locally, the problem is likely that Rails is html escaping your handy helper method. To see if I'm right, try throwing this in your view:
<%= raw(user_log) %>
If that works, you can throw raw in your helper method instead:
def user_log
if logged_in? == false
raw(form_tag session_path, :id => "mform" do
content_tag(:span, content_tag(text_field_tag :email, "email#domain.com"), :class => "memail")+
content_tag(:span, content_tag(password_field_tag :password, "12345678912"), :class => "mpass")+
content_tag(:span, content_tag(submit_tag 'Login'), :class => "mbutton")
end)
else
...
end
end
raw tells Rails that this code is safe and doesn't need to be html escaped.
As a first guess, I might try something like:
if logged_in? == false
...
else
...
end.html_safe
Update: Ok, back to the drawing board.
As a second guess, try this. (And note the extra parameter to content_tag, which required putting the hash in explicit { }...)
def user_log
if logged_in? == false
form_tag session_path, :id => "mform" do
(
content_tag(:span, content_tag(text_field_tag :email, "email#domain.com"), {:class => "memail"}, false)+
content_tag(:span, content_tag(password_field_tag :password, "12345678912"), {:class => "mpass"}, false)+
content_tag(:span, content_tag(submit_tag 'Login'), {:class => "mbutton"}, false)
).html_safe
end
else
...
end
end
end

Including a manual Rails tag in my form's parameters

This question is related to this other question:
Change rails text_field form builder type
I have a JQuery Tools Range in my form, and making it work requires that the input field be of the type "date". Rails doesn't easily allow me to do this so I've used a manual tag as follows:
<% form_for #customer, :url => {:action => "update", :id => #customer} do |f| %>
...
<%= tag(:input, {:type => :range, :value => f.object.travel, :name => "travel", :min => "0", :max => "100" }) %>
...
<% end %>
This tag shows the range slider. It also displays the right value from the database. However, when I submit a change, the "travel" attribute is sent as a general attribute and not under "customer". So, my database doesn't update.
How can I rewrite the tag so it gets included as a "customer" attribute?
Try:
<%= tag(:input, {:type => :range, :value => f.object.travel, :name => "customer[travel]", :min => "0", :max => "100" }) %>