Ajax 表单提交无效

Ajax form submition not working

我正在尝试为表单创建一个 "Save Changes" 按钮,该按钮将通过 ajax 将数据发送到控制器中的更新方法。目的是允许表单用户在不重新加载或重定向表单的情况下保存他们的工作。但是我 运行 遇到了一些问题;我收到以下错误

undefined method `update_incorporation_path'

明确地说,incorporation 是我们正在使用的控制器。下面是我为完成此操作而添加的代码。

以我的观点,我补充说:

<%= button_to "", update_incorporation_path(@incorporation), :remote => true, :method => :post %>

在我的路线中,我添加了:

resources :incorporations do
  member do
    post 'update'
  end
end

更新方法如下所示:

  def update
    if @incorporation.update(incorporation_params)
      if admin_signed_in?
    @incorporations = Incorporation.all.order("created_at DESC")
      else
    @incorporations = current_user.incorporations("created_at DESC")
      end
      render action: "index"
    else
      render 'edit'
    end
  end

完整图如下: edit.html.erb

<%= render 'form' %>

<br/>
<%= link_to "Back", root_path, class: "btn btn-default" %>

_form.html.erb(按钮在底部)

<div id="wrapper" class="active main-content">
  <%= simple_form_for @incorporation do |f| %>
    <!-- Sidebar -->
      <!-- Sidebar -->
    <div id="sidebar-wrapper">
      <ul id="sidebar_menu" class="sidebar-nav">
        <li class="sidebar-brand"><a id="menu-toggle" href="#">Menu<span id="main_icon" class="glyphicon glyphicon-align-justify"></span></a></li>
      </ul>
      <% @sections=[["basic_info", "Basic Info"],["address", "Address"],["equity", "Equity"],["officers","Officers"],["directors", "Directors"],["contractor","Contractors"],["ip","IP"],["shareholders", "Shareholders"]] %>
      <ul class="sidebar-nav" id="sidebar">
    <% @sections.each do |section| %>
      <li><a href="#<%= section[0] %>" class="anchor_link"><span class="sub_icon glyphicon glyphicon-link"></span><%= section[1] %></a></li>
    <% end %>
      </ul>
      <div id="save">Save</div>
    </div>
    <div class="panel-body">
      <div id="basic_info" class="form_section">

    <div class="form-left"><h2>Basic Info</h2></div>
    <div class="form-right">
      <%= f.simple_fields_for :company do |company| %>
        <div class="padded-fields">
          <%= render 'basic_fields', company:company %>
        </div>
      <% end =%>
      <div class="padded-fields">
        <div class="form_subsection">
          <%= f.input :trademark_search, as: :radio_buttons, label: 'Would you like us to do a trademark search and provide advice regarding any issues we identify in relation to the name you have selected?', input_html: { class: 'form-control' } %>
        </div>
      </div>
    </div>
      </div>
      <%= f.simple_fields_for :company do |company| %>

    <div id="address" class="form_section">
      <%= render 'address_fields' , company:company %>
    </div>

    <div id="equity" class="form_section">
      <%= render 'equity_fields' , company:company %>
    </div>

    <div id="officers" class="form_section">
      <div class="form-left"><h2>Officers</h2><br/><p>Please list the officers of the company.</p></div>
      <div class="form-right">
        <div>
          <%= company.simple_fields_for :officers do |officer|%>
            <%= render 'officer_fields', f: officer %>
          <% end =%>
          <%= link_to_add_association 'Add Officer', company, :officers, class: "btn btn-default add-button" %>
        </div>
      </div>
    </div>

    <div id="directors" class="form_section">
      <div class="form-left"><h2>Directors</h2><br/><p>Please list the initial directors of the company.  We recommend an odd number to avoid a deadlocked board.</p></div>
      <div class="form-right">
        <div>
          <%= company.simple_fields_for :people do |person|%>
            <%= render 'person_fields', f: person %>
          <% end =%>
          <%= link_to_add_association 'Add Director', company, :people, class: "btn btn-default add-button" %>
        </div>
      </div>
    </div>

    <div id="contractor" class="form_section">
      <div class="form-left"><h2>Employees Contractors</h2></br><p>Please list all employees, independent contractors and any other individual or entity who will be providing services to the company at the time of incorporation.  Each of these persons should have written agreements with the company.  Please check the box next to each name for whom you would like us to prepare agreements</p></div>
      <div class="form-right">
        <div>
          <%= company.simple_fields_for :contractor_people do |contractor| %>
        <%= render 'contractor_person_fields', f:contractor %>
          <% end =%>
          <%= link_to_add_association 'Add Person', company, :contractor_people, class: "btn btn-default add-button" %>
        </div>
        <div class="form_subsection">
          <div>
        <%= company.simple_fields_for :contractor_orgs do |contractor| %>
          <%= render 'contractor_org_fields', f:contractor %>
        <% end =%>
        <%= link_to_add_association 'Add Company', company, :contractor_orgs, class: "btn btn-default add-button" %>
          </div>
        </div>
      </div>
    </div>

    <div id="ip" class="form_section">
      <div class="form-left">
        <h2>Intellectual Property</h2><br/><p>Please list existing intellectual property (including business plans, software, artwork, inventions, trade secrets and the like) that has been created for use in the company and the name of the person or people who created it.</p>
      </div>
      <div class="form-right">
        <div>
          <%= company.simple_fields_for :ips do |ip| %>
        <%= render 'ip_fields', f: ip %>
          <% end =%>
          <div class="add-field"><%= link_to_add_association 'Add IP', company, :ips, class: "btn btn-default add-button" %></div>
        </div>
      </div>
    </div>

    <div id="shareholders" class="form_section">
      <div class="form-left"><h2>Shareholders</h2><br/><p>Please list all individuals to hold equity in this company.</p></div>
      <div class="form-right">
        <div>
          <%= company.simple_fields_for :shareholders do |shareholder|%>
            <%= render 'shareholder_fields', f: shareholder %>
          <% end =%>
          <%= link_to_add_association 'Add Shareholder', company, :shareholders, class: "btn btn-default add-button" %>
        </div>
      </div>
    </div>

      <% end =%>
    </div>
    <%= f.button :submit, id:"incorporation_submit", class: "btn btn-primary" %>
    <%= button_to "Update", incorporation_path(@incorporation), method: :post, remote: true %>
  <% end =%>
</div>

我想我一定是忘记了什么。非常感谢任何想法。

update 方法的路由是在您编写 resources :incorporations 时默认添加的,因此请将您的路由更改为

resources :incorporations

而你的路径应该是incorporation_path,另外button_to中的方法默认是post,不用写,

将您的 button_to 更改为

<%= button_to "Update", incorporation_path(@incorporation), :remote => true %>

但是,如果您要提交表单,它应该有一个提交按钮而不是 button_to,您的表单应该如下所示

<%= form_for @incorporation, remote: true do |f| %>
  # form content
  <%= f.submit "Submit" %>
<% end %>

希望对您有所帮助!

而不是:

<%= button_to "", update_incorporation_path(@incorporation), 
   :remote => true, :method => :post %>

尝试:

<%= button_to "Update", incorporation_path(@incorporation), 
   method: :post, remote: true %>

在你的路线中:

resources :incorporations

资源附带默认操作索引、新建、创建、编辑、更新、销毁。您不需要手动声明它。

您可以从控制台验证路由。

 rake routes | grep 'incorporations'

您将得到如下输出: 从这里您可以构建更新操作的路径。 希望对您有所帮助:)