I am trying to use linkt_to
( <%= link_to 'address' , boat_path("'+boat_id+'") %>
) in my jquery code but I think because of the concatenation problem it does not work as I want.
Here is my Jquery code;
<script>
(function ( $ ) {
$('#map-canvas').mapSearch({
request_uri: 'locations/show.json',
initialPosition: [ <%= @initlat %> , <%= @initlng %> ],
filters_form : '#filters',
listing_template : function(listing){
var pics = listing.pic.image.thumb.url
var address = listing.loc.address
var boat_id = listing.loc.boat_id
console.log(boat_id) // works fine
return '<div class="listing">'
+ '<h3>'+ '<%= link_to 'address' , boat_path("'+boat_id+'") %>' + '</h3>' // HERE IS THE PROBLEM
+ '<div class="row">'
+ '<div class="col-sm-2">'
+ '<img src= "'+pics+'" >'
+ '</div>'
+ '<div class="col-sm-5">'
+ '<p><strong>Address : </strong>' + listing.address+ '</p>'
+ '<p>'+listing.boat.year+', '+listing.boat.model+' '+listing.boat.captained+'</p>'
+ '<p>Reg Year: ' + listing.boat.year+'</p>'
+ '</div>'
+ '<div class="col-sm-5">'
+ '<p><strong>demo:</strong> '+listing.address+'</p>'
+ '<p><strong>demo:</strong> '+listing.address+'</p>'
+ '</div>'
+ '</div>'
+ '</div>';
},
marker_clusterer : true
});
}( jQuery ));
</script>
First of, I can not get the address, it renders the page by putting there the word address and the boat_id does not work. The link gets http://localhost:3000/boats/'+boat_id+'
instead of something like http://localhost:3000/boats/250
. But when I hardcode the boat_id part it works.
So, how should I change this;
'<%= link_to 'address' , boat_path("'+boat_id+'") %>' to show address itself and the boat_id as number.
Thank you!
而不是红宝石代码,写的是
"<a href='boats/"+boat_id+"'>Address</a>"
boat_id
is a javascript variable - which means it won't be available for use until your javascript code is executed, in the browser.
link_to
is a ruby/rails method, which means it will get executed on the server.
So, at the time your link_to
method is executed, your boat_id
var won't be available.
There's no way around this - you'll have to ditch using link_to
, and generate your URL/link manually in javascript, with plain text.
Let us suppose you want to have a link inside an element say
<div class="test">
</div>
jQuery code to add a link inside div
var boat_id = 2;
$('.test').html("<a href='path/" + boat_id.toString() + "'>boat</a>");
The above jQuery code will create a link inside div.
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.