从bootstrap popover没有在POST中传递参数

时间:2015-02-25 20:04:34

标签: ruby-on-rails twitter-bootstrap

我正在提供一份培训练习列表供教练浏览。如果教练喜欢练习,那么他们可以将它添加到他们的最爱。如果教练有一个以上的教练任务,我会提出一个弹出窗口,要求他们选择将训练练习添加到哪个作业中作为最爱。

培训作业以复选框的形式显示,但是当进行选择并点击保存链接时,我没有将复选框传回我的控制器进行处理。

我的popover设置如下:

 <%= content_tag :button, type: "button", class: "btn btn-default btn-xs", 
                 :data => { container: "body", toggle: "popover", placement: "top", 
                  content: "#{render partial: 'selectassignments', :locals => {activity_id: @assignment.activity_id, drill: drill}}"} do %>
     Favourite
  <% end %> 

呈现我的部分

<%= form_for(drill) do |f| %>
<strong>Apply to <%= Activity.find_by(id: activity_id).name %> assignments: </strong><br>
  <%= hidden_field_tag "drill[assignment_ids][]", nil %>
    <% assignments = Assignment.joins(:coaches).where('user_assignments.user_id = ? and assignments.activity_id = ? and user_assignments.role = ?', current_user.id, activity_id, "coach") %>
    <% assignments.each do |a| %>
        <%= a.name %>
        <%= check_box_tag "drill[assignment_ids][]", a.id, AssignmentDrill.where(drill_id: drill.id, assignment_id: a.id).exists?, id: dom_id(a) %>
    <% end %>
  <br>
  <br>
  <div align="right"><%= link_to "Save", drill_assignmentaction_path(drill, :add_favourite => drill.id), class: "btn btn-xs", method: :post, remote: true %></div>
</div>

我的控制器代码是

def assignmentaction
  puts "processing assignment action"
  assignment_ids = params[:assignment_ids]
  puts "params[] array contains #{params.to_s}"
end

控制台输出显示:

Started POST "/drills/1/drills/assignmentaction?add_favourite=1" for 127.0.0.1 at 2015-02-26 06:19:52 +1030
Processing by DrillsController#assignmentaction as JS
Parameters: {"add_favourite"=>"1", "drill_id"=>"1"}
User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
processing assignment action
params[] array contains {"add_favourite"=>"1", "controller"=>"drills", "action"=>"assignmentaction", "drill_id"=>"1"}

我已在我的控制器中将:assignment_ids列入白名单,并希望看到来自popover的POST。

不确定为什么没有通过?

1 个答案:

答案 0 :(得分:0)

好的 - 最后很容易修复。只需阅读一些文档

我将表单更改为form_tag而不是form_for

<%= form_tag(drill_assignmentaction_path(drill), method: "post") do |f| %>

而不是link_to提交数据用于a - 等待它submit_tag

<%= submit_tag("Save", class: "btn btn-xs", method: :post, remote: true) %>