简体   繁体   English

Rails 远程表单 collection_select onchange 错误

[英]Rails remote form collection_select onchange error

I'm trying to have a remote form automatically submit when a select option is changed.我正在尝试在更改选择选项时自动提交远程表单。 Everything works when I remove remote: true from the form tag, except I don't want the page to reload each time.当我从表单标签中删除remote: true时,一切正常,除非我不希望页面每次都重新加载。

I am using Rails 5.1.4.我正在使用 Rails 5.1.4。

Code代码

<%= form_for @foo, remote: true do |f| %>

    ... fields

    <%= f.collection_select :bar_id, @list, :id, :name,
       {include_blank: '-'}, { onchange: 'this.form.submit();'} %>

<% end %>

Controller控制器

def update
  respond_to do |format|
    if @foo.update(foo_params)
      format.html { redirect_to @foo }
      format.json { render :show, status: :ok, location: @foo }
    else
      format.html { render :edit }
      format.json { render json: @foo.errors, status: :unprocessable_entity }
    end
  end
end

I'm getting the following error when the selection is updated in the view:在视图中更新选择时出现以下错误:

ActionController::InvalidAuthenticityToken ActionController::InvalidAuthenticityToken

I'm assuming the this.form.submit();我假设this.form.submit(); is the problem.是问题所在。 How can I get this form to submit successfully?我怎样才能让这个表格成功提交?

Try to the following simple way adding authenticity_token: true to your form tag like below尝试使用以下简单方法将authenticity_token: true添加到您的表单标签中,如下所示

<%= form_for @foo, remote: true, authenticity_token: true do |f| %>

It should work, at least working for me.它应该有效,至少对我有效。

Updated After Comment评论后更新

You need to work with partial if you use remote: true like if your form in new.html.erb then cut out form part and create a partial for the form like _form.html.erb then render this into new.html.erb like你需要工作,部分如果你使用remote: true像,如果您的形式new.html.erb然后剪下组成部分,并创建一个部分为等形式_form.html.erb然后渲染到这个new.html.erb

new.html.erb

<div id="my-form">
   <%= render partial: "form" %>
</div>

and the create a js.erb file based your main file like new.js.erb and put this line like并根据您的主文件创建一个js.erb文件,例如new.js.erb并将这一行放在

$("#my-form").html("<%= escape_javascript(render("form")) %>");

On your controller add format.js like below在您的控制器上添加format.js如下所示

format.html { redirect_to @foo } 
format.js 
format.json { render :show, status: :ok, location: @foo }

For complete jQuery/AJAX reference with RoR-5 you can read this tutorial it will help to brush up your jQuery/AJAX skill有关 RoR-5 的完整jQuery/AJAX参考,您可以阅读本教程,它将有助于提高您的jQuery/AJAX技能

Hope that will work希望这会奏效

add this line to your form.将此行添加到您的表单中。

<%= tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) %>

and your form should be like this:你的表格应该是这样的:

   <%= form_for @foo, remote: true do |f| %>
        <%= tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) %>
        ... fields

        <%= f.collection_select :bar_id, @list, :id, :name,
           {include_blank: '-'}, { onchange: 'this.form.submit();'} %>

    <% end %>

The authenticity token is a random value generated in your view to prove a request is submitted from a form on your site, not somewhere else.真实性令牌是在您的视图中生成的随机值,用于证明请求是从您网站上的表单提交的,而不是其他地方。 This protects against CSRF attacks这可以防止 CSRF 攻击

You can avoid above error by two ways您可以通过两种方式避免上述错误

1) Add following code in application controller 1)在应用程序控制器中添加以下代码

skip_before_filter :verify_authenticity_token  

Reference Link: http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html参考链接: http : //api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html

2) Add authenticity_token in the form 2)在表单中添加authenticity_token

<%= hidden_field_tag :authenticity_token, form_authenticity_token %>

OR <%= csrf_meta_tags %><%= csrf_meta_tags %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM