开发者

Linking to show view with Ruby in application.js file

开发者 https://www.devze.com 2023-02-17 00:42 出处:网络
I dynamically generate the title of a video from the embedly API in my application.js file wit开发者_StackOverflow中文版h:

I dynamically generate the title of a video from the embedly API in my application.js file wit开发者_StackOverflow中文版h:

$(this).parent().prepend('<p>'+ oembed.title + '<p>');

Bear in mind that code is taken out of context.

Now I want to make the title a link to that video's show view. However, I don't believe I have access to named routes in my application.js file. What should I do?


This is a long way of going about it (hopefully someone else comes up with a better solution), but perhaps in the view where the title is being generated, you can include the URL to the show view in a data- attribute, generated by ERB (Or Haml or whatever). For example:

<div class="video">
  <whatever class="my_embedly_object" data-show-url="<%= video_path(@video) %>" />
</div>

Then you can access the data-show-url via Javascript in your application.js code and use it to generate the link.

url = $(this).attr('data-show-url');
$(this).parent().prepend('<p><a href="' + url + '">' + oembed.title + '</a></p>');
0

精彩评论

暂无评论...
验证码 换一张
取 消