Hi all, I''m using the collection_select helper method in the partial _form.rhtml that is used in the edit.rhtml. When I edit an agent I want to be able to select a manager or no manager at all. In use the additional :prompt option like this: <p><label for="agent_parent_id">Manager</label><br/> <%= collection_select("agent", "parent_id", @agents, :id, :full_name, {:prompt => "Select manager..."}, {:style => "width: 150px"}) %></p> The @agents array comes from the edit action. Basically I retain all the current children of the agent and the agent itself because they can not be a manager: def edit @agent = Agent.find(params[:id]) @agents = Agent.find_all_ordered - @agent.full_set end The older Rails version always displayed the extra :prompt option. I use the following code after the save in my update action: def update @agent = Agent.find(params[:id]) if @agent.update_attributes(params[:agent]) @agent.move_to_child_of(params[:agent][:parent_id]) ... end If no manager was selected then the params[:agent][:parent_id] value is "". I can''t use it as the parameter for the move_to_child_of method when the agent would have no manager. I tried to use the :include_blank => true option but that gave me an exception: Couldn''t find Agent with ID I think it is because the extra blank option <option value=""></option> is becomes also a parameter with as value the empty string "": *Parameters*: {"commit"=>"Edit", "id"=>"1", "agent"=>{"level_id"=>"4", "identification"=>"BKU", "product_id"=>"1", "first_name"=>"Ben", "last_name"=>"Kunnen", "parent_id"=>""}} The question is how do I select a manager (= the parent node) or none at all from a simple select box in my view and use the posted parent_id correctly in my actions? Kind regards, Mark Noten Software engineer ITFC gcv E-mail: mark.noten at itfc.be GSM: +32 (484) 698 333
Jean-Christophe Michel
2007-Mar-20 22:20 UTC
[Betternestedset-talk] Question about selecting a parent
Hi, I see two possible solutions. Le 20 mars 07, ? 22:15, Mark Noten a ?crit :> I''m using the collection_select helper method in the partial > _form.rhtml > that is used in the edit.rhtml. When I edit an agent I want to be able > to select a manager or no manager at all. In use the additional :prompt > option like this: > > <p><label for="agent_parent_id">Manager</label><br/> > <%= collection_select("agent", "parent_id", @agents, :id, :full_name, > {:prompt => "Select manager..."}, {:style => "width: 150px"}) %></p>You could use select instead of collection_select and add your prompt associated with root: <%= select(''agent'', ''parent_id'', @agents.inject([[Agent.root.id, ''Select one...'']]){|all, an_agent| [[an_agent.id, an_agent.full_name]]}) %> so your blank value would match the root of the tree and your node would be moved to be a level one child. (is it what you want ?)> The older Rails version always displayed the extra :prompt option. I > use > the following code after the save in my update action: > > def update > @agent = Agent.find(params[:id]) > if @agent.update_attributes(params[:agent]) > @agent.move_to_child_of(params[:agent][:parent_id]) > ... > endSecond solution: test for empty parent_id here def update @agent = Agent.find(params[:id]) if @agent.update_attributes(params[:agent]) and !params[:agent][:parent_id].empty? @agent.move_to_child_of(params[:agent][:parent_id]) ;-) Jean-Christophe Michel -- symetrie.com Better Nested Set for rails: http://opensource.symetrie.com/trac/better_nested_set