ruby on rails jquery提交表单为js [英] ruby on rails jquery submit a form as js

查看:70
本文介绍了ruby on rails jquery提交表单为js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

<%= form_for :folder_name, :remote => true, :method => "get", :url => {:action 
    => "show_workflow_list"} do |f| %>
  <%= f.select :foldernames, options_for_select(@folders, @folders.first), {}, {:onchange => ("this.form.submit()")}%><br /><br />
  <%= hidden_field_tag 'selected_domain', params[:domain_selected] %>

当我更改下拉列表中的值时,表单的提交方式为:

When I change the value in the dropdown, the form is being submitted as:

Processing by DeploymentGroupController#show_workflow_list as HTML
Parameters: {"utf8"=>"Γ£ô", "folder_name"=>{"foldernames"=>"DETAIL_ADJUSTMENT"

当我添加一个提交按钮而不是如下所示的:onchange=>时:

When I add a submit button instead of a :onchange=> like below:

<%= form_for :folder_name, :remote => true, :method => "get", :url => {:action => "show_workflow_list"} do |f| %>
  <%= f.select :foldernames, options_for_select(@folders, @folders.first)%><br /><br />
  <%= hidden_field_tag 'selected_domain', params[:domain_selected] %>
  <pre><%= f.submit "Submit"%></pre>

请求正在这样提交:

Processing by DeploymentGroupController#show_workflow_list as JS
Parameters: {"utf8"=>"Γ£ô", "folder_name"=>{"foldernames"=>"DETAIL_ADJUSTMENT"

我的show_workflow_list操作中具有以下代码:

I have the following code in my show_workflow_list action:

def show_workflow_list
  //some code
  respond_to do |format|
    format.js
  end
end

,我有一个show_workflow_list.js.erb文件,该文件包含以下内容:

and I have a show_workflow_list.js.erb file which has the following content:

$('#workflow_selection').html("<%=j render "show_workflow_list" %>");

问题是当我将其更改为onchange=> submit时,它正在将动作作为HTML处理:

The problem is when I change it to onchange=> submit, it is processing the action as HTML:

 Processing by DeploymentGroupController#show_workflow_list as HTML

,而不是带有提交按钮的JS:

and not as JS when there is a submit button:

 Processing by DeploymentGroupController#show_workflow_list as JS

所以我返回了406状态错误,并且未呈现show_workflow_list.

so I'm getting back a 406 status error and the show_workflow_list is not being rendered.

更新:

我了解了为什么onchange=>被作为HTML发送.原因是选择的格式为"select(object, method, choices, options = {}, html_options = {})".我要提到的地方":onchange=>this.for.submit" is under html_options={}"这就是为什么将其作为HTML提交的原因. 我需要在同一页面的<div>之间呈现部分_show_workflow_list.html.erb,所以我像这样更改了控制器中的代码:

I understood why onchange=> is being sent as HTML. The reason is that the format for select is "select(object, method, choices, options = {}, html_options = {})". The place i'm mentioning ":onchange=>this.for.submit" is under html_options={}" which is why it is being submitted as HTML. I need to render a partial _show_workflow_list.html.erb between a <div> on the same page, so I changed the code in my controller like this:

def show_workflow_list
  //code here
  respond_to do |format|
    format.html{?}
  end
end

在上面的代码中,我需要在{}format.html中填充一些内容,以便在make_deployment_group.html.erb中的div标记之间呈现部分的_show_workflow_list.

In the code above, I need to fill something in {} the format.html so that it will render the partial _show_workflow_list between the div tags in my make_deployment_group.html.erb.

推荐答案

在JS文件上,您应该这样做:

On a JS file you should do this :

$('#foldernames').change(function() {
  $('#folder_name').submit()
});

在您的控制器中,您应该具有以下内容:

And in your controller you should have this :

def show_workflow_list
  //code here
  render :format => :js
end

更改值时,它应该提交表单,以便将ajax请求发送到您的控制器.您的控制器应呈现js格式并执行show_workflow_list.js.erb.要进行检查,可以使用Firebug之类的JavaScript控制台.

When you change the value, it should submit your form so it should send an ajax request to your controller. Your controller should render a js format and execute your show_workflow_list.js.erb. To check it, you can use a javascript console like firebug.

这篇关于ruby on rails jquery提交表单为js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆