简体   繁体   English

如何调用.js.erb文件而不是.rjs文件

[英]How to call a .js.erb file instead of a .rjs one

I'm working on a migration from Prototype to jQuery and we also have to remove .rjs file when we can. 我正在研究从Prototype到jQuery的迁移,我们还必须尽可能删除.rjs文件。 There is here one issue : 这里有一个问题:

I have got the update_overall_comment.rjs file in the app/views/results folder: 我在app / views / results文件夹中有update_overall_comment.rjs文件:

page.replace 'overall_comment', :partial => 'results/marker/overall_comment', :locals =>  {:result => @result}
page.visual_effect(:highlight, 'overall_comment_text_area', :startcolor => '#BDFCC9')

The _overall_comment.html.erb file in the app/views/results/markers folder: app / views / results / markers文件夹中的_overall_comment.html.erb文件:

<div id="overall_comment">
          <div id="overall_comment_edit">
            <%= form_for @result,
              :remote => true,
              :url => { :action => 'update_overall_comment', :id => @result.id } do |f| %>
              <%= f.text_area :overall_comment,
                :cols => 50,
                :rows => 5,
                :id => 'overall_comment_text_area',
                :onkeydown => "$('overall_comment_submit').enable();" %>
           <div>
             <%= f.submit t(:save_changes),
               :disable_with => I18n.t('working'),
               :disabled => true,
               :id => "overall_comment_submit"%>
           </div>
          <% end %>
    </div>
</div>

And the results.controller.rb file (I just show the relevant function ) in the app/controllers folder: 而app / controllers文件夹中的result.controller.rb文件(我只是显示了相关功能):

def update_overall_comment
  @result = Result.find(params[:id])
  @result.overall_comment = params[:result][:overall_comment]
  @result.save
end

The fist thing I would like to understand is how to transform the .rjs file to a .js.erb on. 我要了解的第一件事是如何将.rjs文件转换为.js.erb。 I removed it and created update_overall_comment.js.erb (in the same folder) which only contains a console.log('...'), and I never saw the console.log called. 我删除了它并创建了update_overall_comment.js.erb(在同一文件夹中),它仅包含console.log('...'),但从未看到console.log被调用。

Do you have any ideas ? 你有什么想法 ?

Thank you in advance. 先感谢您。

rename update_overall_comment.rjs to update_overall_comment.js.erb 重命名update_overall_comment.rjsupdate_overall_comment.js.erb

then in this file: 然后在此文件中:

$('#overall_comment').html("<%= j(render 'results/marker/overall_comment') %>");
$('#overall_comment_text_area').effect("highlight", { color: "#BDFCC9" }, 3000);

ps not sure about the highlight, also for verifying that the js.erb file is called/rendered, I use alert('something here'); ps不确定亮点,也为了验证js.erb文件已被调用/渲染,我使用alert('something here'); instead of console('...'); 而不是console('...'); , but this shouldn't be much of a difference in order to test an ajax call. ,但这对于测试ajax调用应该没有太大的区别。

Thank you very much. 非常感谢你。 I have made some mistakes. 我犯了一些错误。

For instance I thought a line with "//" was a comment and in fact it does a ajax error. 例如,我认为带有“ //”的行是注释,实际上它确实造成了ajax错误。 Also I used partial for the first line and it did't work. 另外,我在第一行使用了partial,但它没有用。

And I thought this js.erb file would inform me about errors but no. 而且我认为这个js.erb文件会告知我有关错误的信息,但没有。 It just stop when it doesn't work without any information. 如果没有任何信息它就无法工作,它就会停止。 So I had to find by myself that my jquery version didn't have the "effect" method. 因此,我不得不自己发现我的jquery版本没有“ effect”方法。

But now it works and your translation of visual_effect work (I just put 1000ms instead of 3000). 但是现在它可以工作了,而您对visual_effect的翻译也可以工作(我只是把1000ms代替了3000ms)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM