我试图创建一个" Save Changes"表单的按钮,用于通过ajax将数据发送到控制器中的update方法。目的是允许表单用户在没有重新加载或重定向的表单的情况下保存他们的工作。但是,我遇到了一些问题;我收到以下错误
未定义的方法`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>
我想我必须忘记一些事情。任何想法都非常感激。
答案 0 :(得分:1)
默认情况下,在您编写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 %>
希望这有帮助!
答案 1 :(得分:1)
而不是:
<%= 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'
您将获得如下输出: 从这里,您可以构建更新操作的路径。 希望它有所帮助:)