Pass parameters between forms in the same RoR view
I have two forms in the same view form_tag and form_for. The first one (form_tag) uses it to do a direct search for the node (parent node). The second one (form_for) uses it to create a node (child node). To be clearer, each node has a variable (parent_id) pointing to the other node (parent node).
My problem is that when I create a child node (with form_for) I don’t know how to assign the parent_id value because I don’t 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
source to share
It doesn't matter how you create your forms (form_for, form_tag, simple_form_for ...). All this will result <form>
in a rendered html template. These forms they "live" on the client side (browser). If you submit a form, then only form elements (elements like <input>, <textarea>, <select>
) are sent to the server .
Not valid for nested tags <form>
, i.e. you shouldn't have a tag <form>
inside another tag <form>
. Indeed, although there are multiple tags on the same page <form>
.
If you want to exchange information between elements <form>
, you can use Javascript to "transfer" values from one <form>
to another.
Let's say you want to know the parent node id when you submit your second form:
- register change listener on parent node id in first view
- on change, read the value, write it to the hidden field in the second form
Assuming you are using jQuery: you may find this useful: https://api.jquery.com/change/
Or you can set the parent node id to a hidden field when it was selected in live search.
Anyway: if it is not present in <form>
, it will not be sent to the server. So the solution is to use a hidden field.
source to share