我在 html 表中嵌入了一个 rails 表单作为一个简单的下拉菜单。我还以这样一种方式实现了表单,即只要下拉菜单中有更改,就会提交表单。但是我遇到了一个我正在尝试解决的路由错误。
表格代码:
<td>
<%= form_for(lead, :action => 'update_lead_status', :html => {:id => 'lead_form'}, :remote => true) do |f| %>
<%= f.select(:status, ["to_call","called","confirmed","lite"], {:selected => lead.status}, :onchange => "$('#lead_form').submit();") %>
<% end %>
</td>
routes.rb 文件:
get 'leads/:id/edit_lead_status' => "leads#edit_lead_status"
put 'leads/:id/update_lead_status'=> "leads#update_lead_status"
Leads_controller.rb 文件:
#PUT
def update_lead_status
@lead = Lead.find(params[:id])
respond_to do |format|
format.js
if @lead.update_attributes(params[:lead])
format.html { redirect_to leads_url, notice: 'Lead was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @lead.errors, status: :unprocessable_entity }
end
end
end
此外,我在表格中的嵌入表单位于 /leads URL,我尝试在成功更新时重定向。
我服务器的日志:
ActionController::RoutingError (No route matches [PUT] "/leads/130"):
status 是我的潜在客户模型中的可访问属性。
请让我知道我哪里出错了?
更新:
所以我尝试了sevenseacat的答案,它调用了我的update_lead_status,但参数错误。我的桌子是这样的:
<% @leads.each do |lead| %>
<tr>
<td><%= lead.id %></td>
<td><%= lead.prefix %></td>
<td><%= lead.name %></td>
<td><%= lead.mobile %></td>
<td><%= lead.phone %></td>
<td><%= lead.category %></td>
<td><%= lead.area %></td>
<td><%= lead.city %></td>
<td><%= lead.updated_at %></td>
<td><%= lead.is_valid %></td>
<td><%= lead.vendor_status %></td>
<td><%= lead.call_tries %></td>
<td>
<%= form_for(lead, url: update_lead_status_path(lead.id), :html => {:id => 'lead_form'}, :remote => true) do |f| %>
<%= f.select(:status, ["to_call","called","confirmed","lite"], {:selected => lead.status}, :onchange => "$('#lead_form').submit();") %>
<% end %>
</td>
<td><%= lead.remarks %><%= link_to lead.remarks, :controller => "leads", :action => "edit_lead_remarks", :id => lead, :remote => true %>
</td>
</tr>
<% end %>
在这里,对于表中的每一行,它都将 id 作为表中第一个潜在客户的 id。