Hello everyone I was wondering how I can get rid of this ugly url that was generated when I clicked a link on my website. It looks like this:
http://www.imaaroncs.com/about?as=AboutMe&class=changeMe&controler=welcome
I want it to just say about but I do not know how to do it with the link_to rails helper this is what I have so far:
<li class="navbar-brand"><%= link_to "About Me", class: "changeMe",controler: "welcome", as: "AboutMe", action: "about"%>
You're sending a few parameters you don't need (the ones after ? are being interpreted as additional query params)
<%= link_to "About Me", action: "about" %>
Try using the literal hash:
<%= link_to "About Me", {class: "changeMe", controller: "welcome", as: "AboutMe", action: "about"} %>
PS. Also, there is double l in controller. You have one.
Related
I need to nest an 'a' element inside of another 'a' element like so:
<a href="www.example.com" class="so-and-so">
<span>something</span>
<i class="an-icon">
</a>
However I need to use the rails 'link_to' helpers instead of plain html. But when I try to nest the link_to helper like this:
<%= link_to "www.example.com", class: "so-and-so" do %>
<%= content_tag :span, "something" %>
<%= link_to "www.another.example.com", class: "another-link" do %>
<%= content_tag :i, class: "an-icon" %>
<% end %>
<% end %>
It outputs html more like this:
<a ...>
<span ...>
</a>
<a class="another-link" ...>...</a>
Any help would be greatly apreciated. Thank you!
EDIT
It turns out that rails was outputting the correct html after all, but the browser is not interpreting it correctly. It it possible to have a link inside of a link?
SOLVED (ISH)
It turns out that nesting <a> elements is not valid HTML, so the browser was just trying to "correct" my code. I ended up just placing the a element outside the other one and using CSS to make it look the same.
Since you need the inner link tag point at a destroy action on a controller. You can trigger the action by the click event of the first link.
$('#first_link').on('click', function() {
$.ajax({
type: "DELETE",
url: YOUR_SECOND_LINK ,
data: {id: YOUR_ID },
success: function(data, textStatus, jqXHR){...},
error: function(jqXHR, textStatus, errorThrown){...}
})
});
This can be achieved by using the <template> element which will prevent the browser from closing an anchor tag before another one is opened.
<%= link_to "www.example.com", class: "so-and-so" do %>
<%= content_tag :span, "something" %>
<template>
<%= link_to "www.another.example.com", class: "another-link" do %>
<%= content_tag :i, class: "an-icon" %>
<% end %>
</template>
<% end %>
I worked this out because I was refactoring Vue components that had nested anchor tags into html.erb files. It is a requirement of each of the individual Vue components that it is wrapped in a <template> tag so I copied it over and voila!
I know that you can add HTML classes to link_to helpers like this:
<%= link_to 'Logout', logout_path, class: 'primary-link-style' %>
But what about the mail_to helper? There doesn't seem to be a way to add a HTML/CSS class like you can with the link_to helper.
The docs only talk about examples of inline styling which I want to avoid.
The method signature from the docs you linked to:
mail_to(email_address, name = nil, html_options = {}, &block)
That third argument, html_options, works just like the same argument of link_to:
<%= mail_to "foo#example.com", "Email me", class: "primary-link-style" %>
I need to change this to HTML and have no idea how to put the delete route in a normal a tag, any ideas? Thanks
<li><%= link_to "Link", user_photo_pin_path(user_id: #user.id, photo_id: #photo.id, id: {{id}} ) , method: :delete, data: { confirm: 'Quieres borrar esto?'} %></li>
You can use something like "pry-rails" or "debugger" gems. Set up "debugger" method in your view and execute any of ActionView:Helpers like link_to or form_tag and see what html returned by this methods.
I have the following code,my problem is when i tried to add or any other html tag inside it as shown its not working and it displayed as normal string.
<%= link_to image_tag(class_door(student_class), onMouseover: "this.src='/assets/open_door.png';", onMouseout: "this.src='/assets/closed_door.png'" ),'javascript:;' ,class:'popovers','data-content'=>"Number of Students: #{student_class.students.count}<br><button>Click to enter</button>" ,'data-original-title'=>"#{student_class.name.capitalize}" %>
also i have tried to add html_safe to be like this
<%= link_to image_tag(class_door(student_class), onMouseover: "this.src='/assets/open_door.png';", onMouseout: "this.src='/assets/closed_door.png'" ),'javascript:;' ,class:'popovers','data-content'=>"Number of Students: #{student_class.students.count}<br><button>Click to enter</button>".html_safe ,'data-original-title'=>"#{student_class.name.capitalize}" %>
but this also wasn't working and giving the same result
Try adding 'data-html'=>true. You'd want something like:
<%= link_to image_tag(class_door(student_class), onMouseover: "this.src='/assets/open_door.png';", onMouseout: "this.src='/assets/closed_door.png'" ),'javascript:;' ,class:'popovers', 'data-html'=>true, 'data-content'=>"Number of Students: #{student_class.students.count}<br><button>Click to enter</button>" ,'data-original-title'=>"#{student_class.name.capitalize}" %>
And if any of the values you are putting into your HTML need to be escaped to prevent injection, you can use "h". Something like: #{h(student_class.name.capitalize)}.
I have a sidebar that contains links to all of a users :shopping_lists. Upon clicking on one of those links, I'd like to render a page showing the :list_items in that particular list. Here's my sidebar partial:
<aside class="sidebar-nav-fixed">
<h1>My Lists</h1>
<% if user_signed_in? %>
<% current_user.shopping_lists.each do |l| %>
<ul>
<%= link_to "#{l.name}", '#' %>
</ul>
<% end %>
<% else %>
<h5><%= link_to "Sign in to manage lists.", new_user_session_path %></h5>
<% end %>
</aside>
My question is: what path would I be putting in place of my current stub link in order to route to the correct list? Thanks in advance!
That will depend on how your routes are setup. I would expect shopping lists to always be in the context of a user, so probably something like this:
<%= link_to l.name, user_shopping_list_path(current_user, l) %>
If shopping lists are a top level route, then probably something like this:
<%= link_to l.name, shopping_list_path(l) %>
There are couple of things you can do, granted your routes are setup correctly:
The easiest is:
link_to "#{l.name}", l
Rails should create a link something similar to http://host/shopping_lists/2
The above is a shorthand for
link_to "#{l.name}", shopping_list_path(l)
To see a list of available routes and methods you can run:
bundle exec rake routes
in the root of your rails app