有关 Rails link_to 和 post 方法的帮助
我需要帮助将学生分配到批次..他们是多对多的关系。
<tbody>
<% Batch.all.each do |b|%>
<tr>
<td><%= b.course.name%></td>
<td><%= b.name %></td>
<td><%= b.section_name%></td>
<td><%= link_to "Add", student_batch_students_path(@student, :batch_id=> b.id), :method=> :post%></td>
</tr>
<%end%>
</tbody>
在我的控制器中
def create
@batch_student = BatchStudent.new(params[:batch_student])
@batch_student.save
end
我的路线
resources :students do
resources :batch_students
end
resources :batches
但是在我的数据库中它使用student_id和batch_id作为null创建它
I need help assigning students to batches.. they are in a many to many relation.
<tbody>
<% Batch.all.each do |b|%>
<tr>
<td><%= b.course.name%></td>
<td><%= b.name %></td>
<td><%= b.section_name%></td>
<td><%= link_to "Add", student_batch_students_path(@student, :batch_id=> b.id), :method=> :post%></td>
</tr>
<%end%>
</tbody>
In my controller
def create
@batch_student = BatchStudent.new(params[:batch_student])
@batch_student.save
end
My routes
resources :students do
resources :batch_students
end
resources :batches
But on my database it creates it with student_id and batch_id as null
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在更新现有批次,但不是创建,因此您应该向
update
操作发出PUT
请求You are updating exist batch, but not creating, so you should make
PUT
request toupdate
actionparams 哈希不包含
:batch_student
哈希,因为您不是从表单提交。参数应该类似于{"student_id" =>; 1、“batch_id”=> 1、“方法”=> “发布”}
。因此,修改您的创建操作如下:
使用 new 的优点是您可以执行
if @batch_student.save
来检查错误。我希望这有帮助。
The params hash doesn't contain a
:batch_student
hash because you are not submitting from a form. The params has should look something like{"student_id" => 1, "batch_id" => 1, "method" => "post"}
.So, modify your create action as follows:
The advantage of using new is you can do a
if @batch_student.save
to check for errors.I hope this helps.
参数和http方法应该在一起
{:batch_id=> b.id, :method=> :帖子}
The parameters and the http method should be together
{:batch_id=> b.id, :method=> :post}