简体   繁体   中英

How to disable HTML URL escaping

I have a Rails application that lists different teams in a list on one the views. The teams are links and used in the URL, for example localhost:3000/teams/chelsea shows "chelsea".

If I have a team called man utd when I click the link it escapes all the spaces so the URL is like:

localhost:3000/teams/<script>man%20utd

but I just it want it to be

localhost:3000/teams/<script>man utd

This is my code:

   <a href="/<%= team["name"].html_safe %>"><%= team["name"] %></a>

I tried using html_safe after reading other questions and also raw but have had no luck with them.

What is the best way to solve this problem?

I wouldn't put any spaces in your URLs - do it as "man-utd" instead. See Spaces in URLs?

There are also more Rails-y ways to write out your links. If your teams are a database model I'd write them out as:

<% @teams.each do |team| %>
   <%= link_to team.name, team %>
<% end %>  

But that will link to /teams/1 and teams/2 etc and you'll have to put in a specific route to map /teams/chelsea to /teams/1.

If you've just done them as separate views for man utd and chelsea then do:

<%= link_to 'Man Utd', :controller => 'teams', :action => 'man-utd' %>
<%= link_to 'Chelsea', :controller => 'teams', :action => 'chelsea' %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM