将下拉值传递给link_to

时间:2013-03-18 20:33:29

标签: ruby-on-rails drop-down-menu link-to

我有一些link_tos,用于根据当前用户在数据库中创建记录。

我希望当前用户能够代表其他用户添加记录 - 例如,添加一个下拉菜单,当前用户可以选择代表他想要创建记录的用户。

我有以下代码,需要调整以满足上述需求 - 请帮助:)

<%= collection_select(:user, :id, User.where(:department_id => current_user.department_id).all, :id, :firstname, :selected => current_user.id) %>

<% @myTemplates.where(:pickable => true).each do |template| %>
    <%= link_to shifts_path(:template_id => template.id, :shift_date => date), :method => :post do %>
        <%= template.type.name %>
    <% end %>
<% end %>

在控制器中我尝试了以下不起作用 - 得到NillClass错误。

params[:user][:id]

编辑:

21:38:38 web.1     | Started POST "/shifts?shift_date=2013-03-19&template_id=2" for 127.0.0.1 at 2013-03-19 21:10:31 +0100
21:38:38 web.1     | Processing by ShiftsController#create as HTML
21:38:38 web.1     |   Parameters: {"authenticity_token"=>"PMpPcxD9f1xh/KmTx2f0b8KZkYu91Q3n3zM7f5ie4zU=", "shift_date"=>"2013-03-19", "template_id"=>"2"}
21:38:38 web.1     |   User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
21:38:38 web.1     |   Template Load (0.1ms)  SELECT "templates".* FROM "templates" WHERE "templates"."id" = ? LIMIT 1  [["id", "2"]]
21:38:38 web.1     | Completed 500 Internal Server Error in 2ms
21:38:38 web.1     | 
21:38:38 web.1     | NoMethodError - undefined method `[]' for nil:NilClass:
21:38:38 web.1     |   app/controllers/shifts_controller.rb:14:in `create'
21:38:38 web.1     |   (gem) actionpack-3.2.11/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
21:38:38 web.1     |   (gem) actionpack-3.2.11/lib/abstract_controller/base.rb:167:in `process_action'
21:38:38 web.1     |   (gem) actionpack-3.2.11/lib/action_controller/metal/rendering.rb:10:in `process_action'
21:38:38 web.1     |   (gem) actionpack-3.2.11/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
21:38:38 web.1     |   (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:436:in `_run__200718806994229589__process_action__4067499974676832569__callbacks'
21:38:38 web.1     |   (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:405:in `__run_callback'
21:38:38 web.1     |   (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
21:38:38 web.1     |   (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:81:in `run_callbacks'
21:38:38 web.1     |   (gem) actionpack-3.2.11/lib/abstract_controller/callbacks.rb:17:in `process_action'
...
...

我的创建操作包含以下内容

@template = Template.find(params[:template_id])

submission_hash = {"user_id"      => params[:user][:id],
                   "type_id"      => @template.type_id,
                   "paytypes_id"  => @template.paytypes_id,
                   "shiftdate"    => params[:shift_date],
                   "shiftstart"   => @template.shiftstart,
                   "shiftend"     => @template.shiftend}

@shift = Shift.new(submission_hash)

1 个答案:

答案 0 :(得分:0)

它并没有真正起作用...一旦页面被渲染,你需要调用JS来改变它。你在这里的选择真的是3

  1. 创建一个ajax端点,用于呈现link_to并使用响应交换容器的内容。您可以通过XMLHTTPRequest

  2. 将您的下拉列表的值发布到端点
  3. 为下拉列表中的每个项目创建一个link_to,并根据onchange事件后下拉列表的值有条件地显示/隐藏它们

  4. 绑定链接本身并根据内容动态构建链接。

  5. jQuery("a#link").bind("click", function (e) {
      e.preventDefault();
      var sel = jQuery("#dropdown").val();
      document.location = "/something/"+sel;
    
    }
    

    根据将此作为帖子的概念,为什么不解析控制器中下拉列表的参数来确定操作。我真的不明白link_to的事情吗?