如何在rails中访问form_tag的响应?

时间:2022-11-24 10:32:48

I have a form which submits number of info in Db.This form is on a modal popup. On click of submit tag, a new modal popup is displayed which has two buttons on click they show the data which the form has submitted. Now the problem is till when the controller call is finished, these buttons do nothing on click. So i want to add a spinner on the form popup on click of the submit button. and when the response comes true/ successful it shows the next popup.

我有一个表单在Db中提交信息。这个表单是在一个模态弹出窗口。单击提交标记后,将显示一个新的模式弹出窗口,其中有两个按钮,单击它们可显示表单已提交的数据。现在问题是控制器调用完成后,这些按钮在点击时什么都不做。所以我想在单击提交按钮时在窗体弹出窗口上添加一个微调器。当响应成功/成功时,它会显示下一个弹出窗口。

my code looks like this:

我的代码看起来像这样:

form:

= form_tag("/system/upload_form", method: 'post', enctype:   'multipart/form-data', remote: true) do
    ...
    ...
    =submit_tag("Submit",id: "submit_button", class: 'btn btn-default')  



controller:

sys = @system.update!(system_params)
respond_to do |format|
  format.js
  format.json { render :json => sys }

js:

$("#submit_button").click(function() {
    $('#modal_define_system').modal('hide'); // current popup
    $('#next-page').modal('show'); // next popup
});

Now i want to know how and where to access the json object or sys value returned from the controller.

现在我想知道如何以及从何处访问从控制器返回的json对象或sys值。

I tried: Adding a class to the form and then

我试过:在表单中添加一个类然后

$('.form_class').bind('ajax:success', function() {
   console.log(sys);
});

But could not succeed.Plese help and advise. Comment if i need to add some more code or explanation.

但无法成功。请帮助和建议。评论我是否需要添加更多代码或解释。

2 个解决方案

#1


1  

You are using form form_tag with remote true, which send the ajax request with js format not the json. If you render json response in js block instead of json in the controller like below, it should work.

您正在使用带有远程true的form form_tag,它使用js格式而不是json发送ajax请求。如果在js块中渲染json响应而不是像下面那样在控制器中渲染json,它应该可以工作。

  respond_to do |format|
     format.js { render :json => sys } #this will be rendered 
     format.json { render :json => sys } #this won't
   end

Alternately you can render javascript file like this

或者你可以像这样呈现javascript文件

$('#results_div').html("<%= @system.try(:title) %>")

in update.js.erb

在update.js.erb中

Hope this helps.

希望这可以帮助。

#2


1  

extending maximus' code, you can use ajax to submit the form which can be more easy for you as per your case. You can try

扩展maximus'代码,您可以使用ajax提交表单,根据您的情况,您可以更轻松地提交表单。你可以试试

     $("#theForm").ajaxSubmit({url: '/system/upload_form', 
     type: 'post'}).success(function(data){
     $('#newModalContainer').html(data);
     });

and in your controller, render html of your modal you want to show with your submitted values which can be passed in instance variables. something like

并在您的控制器中,使用您可以在实例变量中传递的提交值呈现您想要显示的模式的html。就像是

     def upload_form
      #your logic
      render :partial => 'new_modal_to_show'
     end

this is a generic structure and you can change it to your relevant values. Hope it helps :-)

这是一个通用结构,您可以将其更改为相关值。希望能帮助到你 :-)

#1


1  

You are using form form_tag with remote true, which send the ajax request with js format not the json. If you render json response in js block instead of json in the controller like below, it should work.

您正在使用带有远程true的form form_tag,它使用js格式而不是json发送ajax请求。如果在js块中渲染json响应而不是像下面那样在控制器中渲染json,它应该可以工作。

  respond_to do |format|
     format.js { render :json => sys } #this will be rendered 
     format.json { render :json => sys } #this won't
   end

Alternately you can render javascript file like this

或者你可以像这样呈现javascript文件

$('#results_div').html("<%= @system.try(:title) %>")

in update.js.erb

在update.js.erb中

Hope this helps.

希望这可以帮助。

#2


1  

extending maximus' code, you can use ajax to submit the form which can be more easy for you as per your case. You can try

扩展maximus'代码,您可以使用ajax提交表单,根据您的情况,您可以更轻松地提交表单。你可以试试

     $("#theForm").ajaxSubmit({url: '/system/upload_form', 
     type: 'post'}).success(function(data){
     $('#newModalContainer').html(data);
     });

and in your controller, render html of your modal you want to show with your submitted values which can be passed in instance variables. something like

并在您的控制器中,使用您可以在实例变量中传递的提交值呈现您想要显示的模式的html。就像是

     def upload_form
      #your logic
      render :partial => 'new_modal_to_show'
     end

this is a generic structure and you can change it to your relevant values. Hope it helps :-)

这是一个通用结构,您可以将其更改为相关值。希望能帮助到你 :-)