我正在尝试创建一个新的ActiveRecord关系,将current_user的ID和工作分配的ID作为外键,并由用户按一下简单的按钮。用户将接受分配,并且它将在记录中创建该新关联。我已经在控制台中测试了模型,并按预期正确接受/拒绝了工作。
当我单击按钮时,错误提示: ContractsController#create中的ActiveRecord :: RecordNotFound
form_for hidden_field没有正确传递assignment.id,该参数显示: {“ utf8” =>“✓”,“ authenticity_token” =>“ dkGBIQxS06xiu9GR6 + aYLgA6ZArgFD5iipl7HKDCFPdXhDKjX6dSajrLlrwvLlj4IzGutcX1lT51IhM5ksxt4Q =”“”“>”
如您所见,assignment_id为空白。不知道我是否做对了,正确的方法是什么?
我基本上从迈克尔·哈特尔(Michael Hartl)的有关制作Twitter的教程中复制了代码,其概念类似于跟随另一个用户的用户。我已经使用Rails控制台中的find(#)验证了分配记录是否存在。
_accept.html.erb
<%= form_for(current_user.active_contracts.build, local: true) do |f| %>
<div><%= hidden_field_tag :assignment_id, @assignment.id %></div>
<%= f.submit "Accept", class: "btn btn-primary" %>
<% end %>
ContractController.rb
def create
@assignment = Assignment.find(params[:assignment_id])
current_user.accept(@assignment)
redirect_to @assignment
end
assignment#index
<div class="col-md-9">
<h2><%= assignment.event_title %></h2><br>
<strong>Date: </strong><%= assignment.date_start.strftime("%D") %><br>
<strong>Time: </strong><%= assignment.time_start.strftime("%I:%M %p") %> to <%= assignment.time_end.strftime("%I:%M %p") %><br>
<strong>Address:</strong><br>
<address><%= assignment.addressline1 %><br>
<% if !assignment.addressline2.blank? %>
<%= assignment.addressline2 %><br>
<% end %>
<%= assignment.city %>, <%= assignment.state %> <%= assignment.zip %>
</address>
</div>
<% if assignment.user_id == current_user.id %>
<div class="col-md-3">
<%= link_to "Update", title_assignment_path(assignment), class: "btn btn-primary btn-block" %>
</div>
<% end %>
<% if current_user.ssp? %>
<div class="col-md-3">
<%= render 'accept' %>
</div>
<% end %>
</div>
<% end %>
</div>