简体   繁体   中英

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_name').append('<p><%= @response[:resp] %></p>');

If I look at my server console, I see no listing from "build.js.erb" file.

What am I doing wrong, is there a other way to make the "@respond" visible in at view?

Thanks, dot

To render build.js.erb you should do this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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