3

I have two forms in the same view a form_tag and form_for . The first ( form_tag ) use it to make a live search of a node ( parent node ) . The second ( form_for ) use it to create a node ( child node ) . To be clearer , each node has a variable ( parent_id ) pointing to another node ( the parent node ) .

My problem is that when I create the child node (with form_for ) do not know how to assign the value parent_id , because I do not know how to pass the value from one form to another

new.html.erb

<%= form_tag ({controller: "nodes", action: "search"}), :id => "users_search" do %>
  <%= text_field_tag :search, params[:search], :autocomplete => 'off' %>
  <div id='users'>
    <%= render 'users' %>
  </div>
<% end %>

<%=form_for @node do |f| %>
  <%= f.hidden_field :parent_id, :value => @nodo_padre %>
  <%= f.hidden_field :user_id, :value => current_user.id  %>
  <%= f.hidden_field :ocuped, :value => true %>
  <%= f.text_field :custom_node_name %>
  <%= f.check_box :terms_of_service,{}, true,false %>
  <%= f.submit "Pagar", class: "button postfix" %>
<% end %>

_users.html.erb

<% if not @parent.nil? %>
  <% @parent.each do |u| %>
    <%= u.user.email %>
    <%= hidden_field_tag @nodo_padre , u.id %> ...... i think...
<% end %>

Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"nz7hu0sBrv49b2AY/Rw0C6NAK8dNo4ra7YkM5VAL0q+0D9yFsa5/pO3bOOuuXxx+K04MP5dZHNOJzanDToYOmw==",
 "node"=>{"parent_id"=>"",
 "user_id"=>"4",
 "ocuped"=>"true",
 "custom_node_name"=>"Mi primera red",
 "terms_of_service"=>"true"},
 "commit"=>"Pagar"}

nodes_controller.rb

def new
@node = Node.new   
end

def search
@node = Node.new  
@parent = Node.search(params[:search]).where(:ocuped => true)

if not @users.nil?
  if @users.count == 1
    @node_incomplete = @users.nodes.where(" sons < ? AND ocuped = ?",2,true).first
  else
    @node_incomplete = @users.first.nodes.where(" sons < ? AND ocuped = ?",2,true).first
  end
  @son_of_incompleted_node = @node_incomplete.children
end

respond_to do |format|
  format.html
  format.js { render }
end
end

def create
@node = Node.new(node_params)
@node.parent_id = @parent.id
respond_to do |format|
  if @node.save
    format.html { redirect_to @node, notice: 'Node was successfully created.' }
    format.json { render :show, status: :created, location: @node }
  else
    format.html { render :new }
    format.json { render json: @node.errors, status: :unprocessable_entity }
  end
end
end
  • You don't have to pass values from one controller to the other if you use nested forms. Are you familiar with `accepts_nested_attributes_for`? – MarsAtomic Jul 07 '15 at 22:22
  • It's posibble nest a form_tag and form_for ? I considered that option but I did not find documentation for that case. – Facundo A. Díaz Martínez Jul 07 '15 at 22:27
  • I don't know if you can nest a form_tag within form_for, however, I wonder if you have to, since both the parent node and its child nodes have an associated model (meaning nesting one form_for child within form_for parent is fine). And more importantly, I wonder if nested forms work in a self-referential relationship. Is your node.rb is set up like [this](http://stackoverflow.com/questions/18791874/rails-model-has-many-of-itself)? – MarsAtomic Jul 07 '15 at 22:33
  • My problem is that I could not run AJAX ( Live Search ) in form_for . For some reason it does not work with form_for , and works with form_tag , so to avoid major problems I do not aim that way. But if there is no other solution ... – Facundo A. Díaz Martínez Jul 07 '15 at 22:44
  • You have `<%= f.hidden_field :parent_id, :value => @nodo_padre %>` but where is `@nodo_padre` coming from? Can you make a relationship in the models to allow `@node.parent` to work? – Beartech Jul 08 '15 at 03:46

1 Answers1

1

It does not matter how you build your forms (form_for, form_tag, simple_form_for...). This will all result in a <form> in the rendered html template. Those forms they "live" on the client side (browser). If you submit a form, then only the form elements (elements like <input>, <textarea>, <select>) are submited to the server.

It's not valid to nest <form> tags, that is you must not have a <form> tag inside another <form> tag. It is valid though to have multiple <form> tags on single page.

When you want to share information between <form> elements, then you can use Javascript to "transfer" the value from one <form> to another.

Say you want to know the parent node id when the second form is submitted:

  • register a change listener on the parent node id element in the first form
  • on change, read the value, write it to a hidden field on the second form

Assuming you use jQuery: you might find this useful: https://api.jquery.com/change/

Or you can set the parent node id to the hidden field when it's selected in the live search.

In any case: unless it is present in the <form> it won't be submitted to the server. So the solution is to use a hidden field.

Pascal
  • 8,464
  • 1
  • 20
  • 31