2016-04-14 4 views
0

remote:trueとフォームを提出でき、ajaxで正常に送信されます。しかし、私はJSとの応答(成功またはエラー)を表示したいですが、私はできません。Rails 4 remote trueとresponse js

は、ここに私のフォーム+ JSです:

   = simple_form_for @widget, :url=> template_save_widget_manager_template_url(id:widget.id),:format => :js, remote: true do |f| 
       = f.input :title, label: "Titre ?" 
       = f.input :active, label: "Titre ?" 
       = f.input :where, label: "Ou souhaitez-vous afficher ce bloc ?", collection: @where 
       = f.button :submit, class:'btn btn-primary' 
       - content_for :script do 
    javascript: 
     $(document).ready(function(){ 
      $("#new_clan_templates_widgets_build").on("ajax:success", function (e, data, status, xhr){ 
      alert('test'); 
      }); 
     }) 

そして、私のコントローラで私はジュストrender falseを追加しました。

JavaScriptコードが機能しません、なぜですか?

+0

はあなたがcreate.js.erb –

+0

の中に置く必要があります。ここではソリューションです.js.erbが動作していません。Chromeでは、「ネットワーク」応答のプレーンテキスト「alert( "test")」:/ – Shinix

答えて

0

これで問題なく動作します。行動に、私は「警告( 『テスト』)を置くi'have試したはい、私は私のコントローラに追加した

class ExampleController < ApplicationController 
    layout proc{|c| c.request.xhr? ? false : "application" } 
    def create 
     respond_to do |format| 
     format.js { render :layout => !request.xhr? } 
     end 
    end 
0
this is my code for creating a post with remote 
<div class="row"> 

<div class="col-md-6"> 
<%= form_for(@blog, html: {role: 'form'}, remote: true) do |f| %> 
    <% if @blog.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@blog.errors.count, "error") %> prohibited this blog from being saved:</h2> 

     <ul> 
     <% @blog.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="form-group"> 
    <%= f.label :title %><br> 
    <%= f.text_field :title, class: "form-control" %> 
    </div> 
    <div class="form-group"> 
    <%= f.label :content %><br> 
    <%= f.text_area :content, class: "form-control" %> 
    </div> 



     <%= f.hidden_field :user_id, value: current_user.id %> 



    <%= f.submit nil, class: "btn btn-primary" %> 

<% end %> 
</div> 
</div> 

and this is my create action in controller 
def create 
    @blog = Blog.new(blog_params) 

    respond_to do |format| 
     if @blog.save 
     format.html { redirect_to @blog, notice: 'Blog was successfully created.' } 
     format.json { render :show, status: :created, location: @blog } 
     format.js{} 
     else 
     format.html { render :new } 
     format.json { render json: @blog.errors, status: :unprocessable_entity } 
     format.js { } 
     end 
    end 
    end 

this is response file after success 
<% if @blog.new_record? %> 
alert('errors'); 
<% else %> 

    alert('post created successfully'); 
    window.location = '<%= blog_path(@blog) %>' 
<% end %> 
関連する問題