ajax获取js请求(rails中的js.erb) - url /命名问题

时间:2011-03-04 01:38:40

标签: ruby-on-rails ajax ruby-on-rails-3 jquery

在我的个人资料/ index.html.haml中, 我有

:javascript
  $(document).ready(function(){
    $('#clickablething').click(function() {
      $.get('/profile', {**passed in json data**}, null, 'script');
    })
  });

在发生点击事件时调用我的index.js.erb。但是我想知道如何重命名.js.erb文件。只有/profile(或./profileprofile)才能专门致电index.js.erb。如果我重命名文件profile.js.erb它不起作用(我已尝试将网址设置为profile.jsprofile.js.erbprofile/profile.js和其他变体,但这些都无效要求作为萤火虫表演。

基本上我想说出index.js.erb profile.js.erb或其他名称,我想知道如何在get电话中引用它。

谢谢!

编辑:这对我来说非常重要,因为我有2个以上的js.erb文件,我想在不同的事件发生时通过ajax GET调用。所以我不能只坚持一个index.js.erb。我该怎么做呢?什么是onescript.js.erbanotherscript.js.erb的ajax调用?

3 个答案:

答案 0 :(得分:0)

索引操作默认情况下为index.html.haml或index.js.erb呈现,但您可以告诉它渲染profile.js.erb,如果这是您想要的...

您的get操作与文件rails服务无关。将您的控制器更改为以下内容:

def index
    respond_to do |format|
        format.js { render 'profile/profile' }
    end    
end

答案 1 :(得分:0)

您对/profile/profile.js有正确的路线吗?

resources :profiles
  get :profile
end

完成后,您可以再试一次。

答案 2 :(得分:0)

我决定将所有内容放在index.js.erb中,并且只运行我需要的部分...