[英]Use Link_to in Jquery code Rails
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. 我正在尝试在我的jquery代码中使用
linkt_to
( <%= link_to 'address' , boat_path("'+boat_id+'") %>
),但是我认为由于连接问题,它无法按我的要求工作。
Here is my Jquery code; 这是我的Jquery代码;
<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. 首先,我无法获取地址,它通过在其中放置单词address来呈现页面,而boat_id无法正常工作。 The link gets
http://localhost:3000/boats/'+boat_id+'
instead of something like http://localhost:3000/boats/250
. 该链接获取
http://localhost:3000/boats/'+boat_id+'
而不是类似http://localhost:3000/boats/250
。 But when I hardcode the boat_id part it works. 但是当我对boat_id部分进行硬编码时,它就可以工作。
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. boat_id
是一个javascript变量-这意味着在浏览器中执行您的javascript代码之前,该变量将不可用。
link_to
is a ruby/rails method, which means it will get executed on the server. link_to
是ruby / rails方法,这意味着它将在服务器上执行。
So, at the time your link_to
method is executed, your boat_id
var won't be available. 因此,在执行
link_to
方法时,您的boat_id
var将不可用。
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. link_to
此问题-您必须使用link_to
,并使用纯文本在javascript中手动生成您的URL /链接。
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 jQuery代码在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. 上面的jQuery代码将在div中创建一个链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.