我将以下表单作为部分工作,我正在尝试对承包商模型进行部分调用,并且我还想传递当前页面的模型 ID,即报价 ID。
它在这一行失败 <%= hidden_field_tag :quote_id, @quote.id %> 'call id for nil'
我尝试创建手动路线并将搜索放在单独的方法上,但随后出现模板错误,因此我现在将其留在索引方法中。
show.html.erb 中的表格:
<%= form_tag quotes_path, :method => 'get', :id => "contractors_search" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<br><br><br>
<div id="contractors"><%= render 'contractors' %></div>
<% end %>
_contractors.html.erb
<table>
<% @contractors.each do | contractor | %>
<tr>
<td><%= contractor.firstname %></td>
<td>
<%= form_tag (quote_add_contractor_path) do %>
<%= hidden_field_tag :quote_id, @quote.id %>
<%= hidden_field_tag :contractor_id, contractor.id %>
<%= submit_tag "Add" %>
<% end %>
</td>
</tr>
<% end %>
</table>
索引.js.erb
$("#contractors").html("<%= escape_javascript(render :partial => "contractors") %>");
控制器:
def index
@quotes = Quote.all
@contractors = Contractor.search(params[:search])
end
def add_contractor
@quote = Quote.find(params[:quote_id])
@contractor = Contractor.find(params[:contractor_id])
@quote.contractors << @contractor
if @quote.save
redirect_to @quote, notice: "contractor was added"
else
render :show, notice: "Sorry, something went aweful"
end
end