【问题标题】:Ajax form submition not workingAjax 表单提交不起作用
【发布时间】:2015-02-02 06:29:54
【问题描述】:

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

未定义的方法`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>

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

【问题讨论】:

    标签: ruby-on-rails ajax forms ruby-on-rails-4 routes


    【解决方案1】:

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

    resources :incorporations
    

    你的路径应该是incorporation_pathbutton_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 %>
    

    希望这会有所帮助!

    【讨论】:

    • 您好 RSB,感谢您的回复!我按照您的建议更改了按钮和路线,并解决了错误消息。但是,我遇到了提交表单和重定向的问题。我创建此按钮的目标是,它只是将数据保存在表单上并保持在同一视图上,而无需重定向或重新加载。我摆脱了控制器中的render action: "index" 并得到了Template is missing 错误。我尝试添加一个`render :nothing => true`,但是当我单击按钮时,这只是给了我一个空白页。有什么想法可以纠正这个问题吗?再次感谢。
    • 如果您能发布表单代码,我会对我有所帮助
    • 我在上面添加了视图和部分
    【解决方案2】:

    代替:

    <%= 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'
    

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

    【讨论】:

    • 嗨,阿杰,感谢您的回答!我按照您的建议更改了按钮和路线,并解决了错误消息。但是,我遇到了提交表单和重定向的问题。我创建这个按钮的目标是它可以简单地将数据保存在表单上并保持在同一个视图上,而无需重定向或重新加载。关于如何做到这一点的任何想法?非常感谢!
    • 在您的控制器中执行此操作删除所有 redirect_to 语句。由于表单是使用 remote: true 选项提交的,因此这将执行 ajax 提交。
    • @neanderslob,当我看到你的更新操作时,你有渲染语句。更新完成后,您不想更改视图中的任何内容,然后只需删除所有渲染和重定向行。如果您想要立即通知用户任何错误/成功消息,那么您可以在您的视图文件夹中有一个 update.js.erb。这个 js 文件将在同一页面中保存您的快速通知。 :)
    • @neanderslob ,点击此链接。这显示了您在保存记录后如何处理任何进一步的交互。 stackoverflow.com/questions/28199634/…
    • 我曾认为这可能是问题所在,但是当我摆脱 render action: "index" 并得到 Template is missing 错误时。我尝试添加一个`render :nothing => true`,但是当我单击按钮时,它只是给了我一个空白页。
    猜你喜欢
    • 2011-11-18
    • 1970-01-01
    • 2011-02-12
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多