2013-10-19 39 views
0

我有一个new.html.slim页面如何添加形式AJAX

_form.html.slim形式

= form_for (@text_input), remote: true do |f| 
.field 
    = f.text_field :title 
    ... 

new.html.slim

h1 new text_input 
== render 'form' 
... 
a.btn type='button' href = '#' 
    | add another text input 

如何在点击按钮后使用AJAX将另一个表单添加到此页面?

回答

0

首先,你确定需要AJAX吗?如果您只想添加其他字段,请使用简单的jQuery。

如果您需要服务器端的响应,并确信您的AJAX,请将AJAX请求编写为按钮的操作。为了实现它,按钮

btn type='button' id='add_form_button' 

添加ID在JavaScript文件的视图与AJAX请求

$(function(){ 
    $("#add_form_button").click(function(){ 
     $.get("ajax/result",function(content){ 
      #in this function should be placement of new form 
      #For example, as that 
      $("#place_for_field").append(content); 
     } 
    } 
} 

你应该航线“AJAX /结果”添加处理程序事件控制器和功能。在config/routes.rb中

get 'ajax/result', to: "controller_name#function_name" 

在带有controller_name的函数中带有function_name的控制器中写入逻辑和响应。你也需要该功能的视图。