jquery - Cannot perform ajax call inside javascript function using rails 3 -


i have form , want call rails controller method using ajax call javascript function only. failed this. please check code below.

payment.html.erb:

<script type="text/javascript"> function check_dropdown(){    $.ajax({     url: "/payments/check_type",     type: 'get'   }); } </script>   <%= form_for :payment,:url => {:action => "check_type" },remote: true |f| %>     <div class="totalaligndiv">       <div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">type :</div></span>       <%= f.select(:s_catagory,options_for_select([['wood','wood'],['puja samagree','puja samagree'],['sweeper','sweeper'],['photo grapher','photo grapher'],['burning assistant','burning assistant']],selected: "type"),{},{:class => 'form-control',:onchange => 'check_dropdown();'}) %>       </div>       <div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">select vendor :</div></span>       <div id="name-option">       </div>       </div>       <div class="clearfix"></div>       <div class="tbpaddingdiv1 text-center">         <button type="submit" class="btn btn-success">submit</button>       </div>     </div>     <% end %> 

application.html.erb:

<!doctype html> <html> <head>   <title>swargadwarapuri</title>   <script src="/assets/moment-with-locales.js"></script>   <%= stylesheet_link_tag    "application", :media => "all" %>   <%= javascript_include_tag "application" %>   <%= csrf_meta_tags %>   <script src="/assets/bootstrap.min-2.0.js"></script>    <script src="/assets/bootstrap-datetimepicker.js"></script>    <link rel="icon" href="/assets/favicon.png" type="image/x-icon" />   <link rel="stylesheet" href="/assets/bootstrap-2.0.css"> <link rel="stylesheet" href="/assets/plugins-2.0.css"> <link rel="stylesheet" href="/assets/main-2.0.css"> <link rel="stylesheet" href="/assets/themes-2.0.css"> <link rel="stylesheet" href="/assets/bootstrap-datetimepicker.css"> </head> <body class="login no-animation" > <%= yield %> <footer> <div class="pull-right"> maintained <i class="fa fa-pencil text-danger"></i> <strong><a href="http://www.oditeksolutions.com" target="_blank">oditek solutions</a></strong> </div> <div class="pull-left"> <span id="year-copy"></span> &copy; <strong><a href="javascript:void(0)" target="_blank">swargadwar, puri municipality</a></strong> </div> </footer> </div> <a href="#" id="to-top"><i class="fa fa-chevron-up"></i></a> <script type="text/javascript" src="/assets/bootstrap-filestyle.min.js"></script> <script src="/assets/plugins-2.0.js"></script> <script src="/assets/main-2.0.js"></script> </body> </html> 

routes.rb:

 "payments/check_type" => "payments#check_type" 

payments_controller.rb:

class paymentscontroller < applicationcontroller     def payment         @payment=vendor.new         respond_to |format|             format.html             format.js         end     end     def check_type         if params[:payment][:s_catagory]             @payment=vendor.find_by_s_catagory(params[:payment][:s_catagory])             @v_name=vendor.where(:s_catagory =>params[:payment][:s_catagory] ).pluck(:v_name)         end     end end 

i want when onchange event fired check_type method called.

i want when onchange event fired check_type method call.

app/controllers/payments_controller.rb:

class paymentscontroller < applicationcontroller   def payment     @vendor = vendor.new     end    def check_type     puts "*****execution has reached here"  #this appear in window typed: rails server     render plain: "good" #return in response ajax request   end    def create  #submit form here?   end end 

app/views/payments/payment.html.erb:

<script>  function check_dropdown(){    $.get("/payments/check_type", function(data_from_server) {     alert("the server responded with: " + data_from_server);       });  }  </script>  <%= form_for(@vendor, url: {action: "create" }, method: "post") |f| %>    <%= f.select(         :s_category,         options_for_select([             ['wood','wood'],             ['puja samagree','puja samagree'],             ['sweeper','sweeper'],             ['photographer','photographer'],             ['burning assistant','burning assistant'] ], "sweeper"),         {},         {               :class => 'form-control',             :onchange => 'check_dropdown()'         }   )%>    <%= f.submit %>  <% end %> 

which produces html:

<!doctype html> <html> <head>   ...   ...   <meta charset="iso-8859-1">   <title>whatever</title>   <meta name="csrf-param" content="authenticity_token" />   <meta name="csrf-token" content="hvsxestq8pl8f6xojzplqwf1vgd9nanbkmjbjm/u6swmoee4f5zpfimfc3ikp415/iuujhq+d0yxvyxfvvu7sg==" /> </head> <body>  <div>application layout</div>  <script>  function check_dropdown(){    $.get("/payments/check_type", function(response_from_server) {     alert("the server responded with: " + response_from_server);       });  }  </script>  <form class="new_vendor"        id="new_vendor"        action="/payments/create"        accept-charset="utf-8"        method="post">    <input name="utf8"           type="hidden"           value="&#x2713;" />   <input type="hidden"           name="authenticity_token"           value="zhpnwgmyhbfr5ay7ikxsmjcojsfkd4eqp72i6btfii/ejr0bwu4fma7tc42psbriqtblyspvi4ecgmwsxcrz8q==" />    <select class="form-control"            onchange="check_dropdown()"            name="vendor[s_category]"            id="vendor_s_category">      <option value="wood">wood</option>     <option value="puja samagree">puja samagree</option>     <option selected="selected" value="sweeper">sweeper</option>     <option value="photographer">photographer</option>     <option value="burning assistant">burning assistant</option>    </select>    <input type="submit" name="commit" value="create vendor" /> </form>  </body> </html> 

and if don't want inline javascript onchange in html, used considered bad practice, can this:

app/views/payments/payment.html.erb:

<%= form_for(@vendor, url: {action: "create" }, method: "post") |f| %>    <%= f.select(         :s_category,         options_for_select([             ['wood','wood'],             ['puja samagree','puja samagree'],             ['sweeper','sweeper'],             ['photographer','photographer'],             ['burning assistant','burning assistant'] ], "sweeper"),         {},         {:class => 'form-control'}   )%>    <%= f.submit %>  <% end %>  <script>  function check_dropdown(){    $.get("/payments/check_type", function(data_from_server) {     alert("the server responded with: " + data_from_server);       });  }  $("#vendor_s_category").on("change", check_dropdown);  </script> 

config/routes.rb:

test1::application.routes.draw   'payments/check_type'   'payments/payment'   post 'payments/create'  end 

db/migrate/20150424194916_create_vendors.rb:

class createvendors < activerecord::migration   def change     create_table :vendors |t|       t.string :s_category       t.string :location        t.timestamps null: false     end   end 

after selecting different choice default choice in <select>, see following in server window:

started "/payments/check_type" ::1 @ 2015-04-24 22:03:45 -0600 processing paymentscontroller#check_type */* *****execution has reached here   rendered text template (0.0ms) completed 200 ok in 3ms (views: 2.2ms | activerecord: 0.0ms) 

and webpage launch alert() displaying text:

the server responded with: good


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -