2013-03-18 22 views
0

我有很多link_tos,用於基於當前用戶在數據庫中創建記錄。通過增加一個下拉菜單,在當前用戶可以選擇他想要代表創建記錄該用戶前 -通行證下拉值LINK_TO

我想當前用戶能夠代表其他用戶的添加記錄。

我有下面的代碼,這需要進行調整,以滿足上述需求 - 任何幫助,請:)

<%= 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) 

回答

0

它並沒有真正以這種方式工作......一旦呈現頁面時需要調用JS改變它。這裏的選項真的是3

  1. 創建一個ajax端點來渲染link_to並用響應換出容器的內容。你可以通過XMLHttpRequest

  2. 發表您的下拉列表中的值平變化的端點在下拉列表,並顯示每一個項目創建的link_to /隱藏有條件的onchange事件後,基於下拉的價值

  3. 綁定鏈接本身並基於內容動態構建鏈接。

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

} 

根據這個做爲帖子的概念,爲什麼不只是解析控制器中的下拉列表參數來確定操作。我真的不明白link_to的東西?

+0

謝謝..我如何解析控制器下拉的帕拉姆?如果我無法傳遞link_to命令,如何從控制器中檢索選定的下拉列表值。希望我的問題是有道理的:) – Twiddr 2013-03-19 19:18:50

+0

,要是你給它在select_tag名稱字段這將是params哈希表 – 2013-03-19 19:48:50

+0

利用Just編輯我的問題。在控制器中添加了collection_select標籤和params調用。我似乎無法使其工作。有任何想法嗎? – Twiddr 2013-03-19 20:14:24