0

我有多个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)
4

1 回答 1

0

It doesn't really work this way... once the page is rendered you need to call JS to change it. Your options here are really 3

  1. Create an ajax endpoint that renderes the link_to and swap out the content of a container with the response. You could post the value of your dropdown onchange to the endpoint via XMLHTTPRequest

  2. Create a link_to for every item in your dropdown and show/hide them conditionally based on the value of the dropdown after an onchange event

  3. Bind the link itself and consturct it dynamically based on the content.

.

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

}

Per the concept of doing this as a post, why not just parse the param of the dropdown in your controller to determine the action. I don't really understand the link_to thing?

于 2013-03-18T20:40:22.393 回答