最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to have a js.erb partial in rails 3.0? - Stack Overflow

programmeradmin1浏览0评论

If it is possible, how can I run it from another js.erb-file?

Trying to keep things DRY!

EDIT: Explanation: I have a js.erb routine in search/index.js.erb. I want to use the same routine in location/index.js.erb. Therefore I wondered if I could make a partial containg js.erb code, so that I don't have to copy the routine from the first index.js.erb file to the other one.

If it is possible, how can I run it from another js.erb-file?

Trying to keep things DRY!

EDIT: Explanation: I have a js.erb routine in search/index.js.erb. I want to use the same routine in location/index.js.erb. Therefore I wondered if I could make a partial containg js.erb code, so that I don't have to copy the routine from the first index.js.erb file to the other one.

Share Improve this question edited Jan 25, 2012 at 21:21 Johan Hovda asked Jan 25, 2012 at 21:07 Johan HovdaJohan Hovda 8552 gold badges10 silver badges23 bronze badges 2
  • can you be a little more specific? – samdeV Commented Jan 25, 2012 at 21:12
  • Added explanation to the original post – Johan Hovda Commented Jan 25, 2012 at 21:21
Add a ment  | 

2 Answers 2

Reset to default 7

YES. you can have a mon partial (say in a folder: app/views/partials) and use that partial's path inside your *.js.erb file like this <%= render :partial => path_to_partial, :locals => { ....} %>

If you want to extract the html string out of a partial you can also use

render_to_string :partial => path_to_partial

Make sure your rendered file begins with an underscore (i.e.:

if you intend to

<%= render :partial => 'myroutine.js.erb', :locals => { ....} %>

the file should be named _myroutine.js.erb)

发布评论

评论列表(0)

  1. 暂无评论