简体   繁体   English

js.erb文件在控制器中的response_to之后将不会加载

[英]js.erb file won't load after respond_to in controller

i need two buttons in my view but only one should use remote, so i decided to use a link and a submit button in the following way: 我在视图中需要两个按钮,但只有一个应使用远程按钮,因此我决定以以下方式使用链接和提交按钮:

view 视图

  <%= form_for(@new_word , :url => {:action => "create"}) do |f| %>

  ... skipped code  ...

  <%= link_to("build word", '#', :class => "build", "data-button" => "build") %>

   ... skipped code ...
  <div id="build_name"></div>

  <%= submit_tag l(:btn_save), :name => 'btn_save' %>

  <% end %>


 <script type="text/javascript">
  jQuery(".build").click(function(){
        var form_value = jQuery('#new_nc_project').serialize();  
        var button = $(this).data("button");    
        var content = $('#nc_project_keyword_tokens').val(); 
        if(button == "build"){
          $.ajax({
            type:'POST', 
            url: '<%= url_for :action => 'build' %>', 
            data: $.param({keyword_tokens: content})
          });
        }
     });
 </script>

controller 调节器

def build
@response = {resp: "text"} //just example text for testing
respond_to do |format|
  format.json { render json: @response }
  format.js   { render js: @response }
end

end 结束

build.js.erb build.js.erb

$('#build_name').append('<p><%= @response[:resp] %></p>');

If I look at my server console, I see no listing from "build.js.erb" file. 如果查看服务器控制台,则看不到“ build.js.erb”文件的列表。

What am I doing wrong, is there a other way to make the "@respond" visible in at view? 我在做什么错,还有其他方法可以使“ @respond”在视图中可见吗?

Thanks, dot 谢谢点

To render build.js.erb you should do this 要渲染build.js.erb您应该这样做

respond_to do |format|
  format.json { render json: @response }
  format.js # this will render build.js.erb by default.
end

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

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